CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
10
Task/Concurrent-computing/Python/concurrent-computing-1.py
Normal file
10
Task/Concurrent-computing/Python/concurrent-computing-1.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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
|
||||
>>>
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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()
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import threading
|
||||
import random
|
||||
|
||||
def echo(text):
|
||||
print(text)
|
||||
|
||||
for text in ["Enjoy", "Rosetta", "Code"]:
|
||||
threading.Timer(random.random(), echo, (text,)).start()
|
||||
14
Task/Concurrent-computing/Python/concurrent-computing-4.py
Normal file
14
Task/Concurrent-computing/Python/concurrent-computing-4.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import random, sys, time
|
||||
import threading
|
||||
|
||||
lock = threading.Lock()
|
||||
|
||||
def echo(s):
|
||||
time.sleep(1e-2*random.random())
|
||||
# use `.write()` with lock due to `print` prints empty lines occasionally
|
||||
with lock:
|
||||
sys.stdout.write(s)
|
||||
sys.stdout.write('\n')
|
||||
|
||||
for line in 'Enjoy Rosetta Code'.split():
|
||||
threading.Thread(target=echo, args=(line,)).start()
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
from __future__ import print_function
|
||||
from multiprocessing import Pool
|
||||
|
||||
def main():
|
||||
p = Pool()
|
||||
p.map(print, 'Enjoy Rosetta Code'.split())
|
||||
|
||||
if __name__=="__main__":
|
||||
main()
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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()
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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()])
|
||||
Loading…
Add table
Add a link
Reference in a new issue