Concurrent computing: Difference between revisions

Updated whitespace, removed versions for old code, and removed code using external libraries as standard libraries allows execution of code. It's suspicious that three standard libraries do the job, so either the task or the code isn't precise enough.
m (Removed output as specified in task and updated whitespace.)
(Updated whitespace, removed versions for old code, and removed code using external libraries as standard libraries allows execution of code. It's suspicious that three standard libraries do the job, so either the task or the code isn't precise enough.)
Line 1,583:
 
=={{header|Python}}==
{{works with|Python|3.7}}
Using asyncio module (I know almost nothing about it, so feel free to improve it :-)):
<lang python>import asyncio
 
 
async def print_(string: str) -> None:
print(string)
 
 
async def main():
strings = ['Enjoy', 'Rosetta', 'Code']
coroutines = map(print_, strings)
await asyncio.gather(*coroutines)
 
 
if __name__ == '__main__':
asyncio.run(main())</lang>
 
{{works with|Python|3.2}}
 
Using the new to Python 3.2 [http://docs.python.org/release/3.2/library/concurrent.futures.html concurrent.futures library] and choosing to use processes over threads; the example will use up to as many processes as your machine has cores. This doesn't however guarantee an order of sub-process results.
<lang python>Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit (AMD64)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>> from concurrent import futures
>>> with futures.ProcessPoolExecutor() as executor:
... _ = list(executor.map(print, 'Enjoy Rosetta Code'.split()))
...
Enjoy
Rosetta
Code
>>></lang>
 
{{works with|Python|2.5}}
 
<lang python>import threading
import random
def echo(text):
print(text)
threading.Timer(random.random(), echo, ("Enjoy",)).start()
threading.Timer(random.random(), echo, ("Rosetta",)).start()
threading.Timer(random.random(), echo, ("Code",)).start()</lang>
 
Or, by using a for loop to start one thread per list entry, where our list is our set of source strings:
 
<lang python>import threading
import random
 
def echo(text):
print(text)
 
for text in ["Enjoy", "Rosetta", "Code"]:
threading.Timer(random.random(), echo, (text,)).start()</lang>
 
=== threading.Thread ===
<lang python>import random, sys, time
import threading
 
lock = threading.Lock()
 
def echo(s):
time.sleep(1e-2*random.random())
Line 1,650 ⟶ 1,602:
sys.stdout.write(s)
sys.stdout.write('\n')
 
for line in 'Enjoy Rosetta Code'.split():
threading.Thread(target=echo, args=(line,)).start()</lang>
 
=== multiprocessing ===
 
{{works with|Python|2.6}}
<lang python>from __future__ import print_function
from multiprocessing import Pool
 
def main():
p = Pool()
p.map(print, 'Enjoy Rosetta Code'.split())
 
if __name__=="__main__":
main()</lang>
 
=== twisted ===
<lang python>import random
from twisted.internet import reactor, task, defer
from twisted.python.util import println
 
delay = lambda: 1e-4*random.random()
d = defer.DeferredList([task.deferLater(reactor, delay(), println, line)
for line in 'Enjoy Rosetta Code'.split()])
d.addBoth(lambda _: reactor.stop())
reactor.run()</lang>
 
=== gevent ===
<lang python>from __future__ import print_function
import random
import gevent
 
delay = lambda: 1e-4*random.random()
gevent.joinall([gevent.spawn_later(delay(), print, line)
for line in 'Enjoy Rosetta Code'.split()])</lang>
 
=={{header|Racket}}==