How to execute multiple python commands in parallel in a python script? As a simple example, I have several sleep commands:
time.sleep(4) time.sleep(6) time.sleep(8)
I want all of the above to be done in parallel. I expect the control to return when 8 seconds have passed (which is the maximum number of all berths above). Real teams will be different, but you want to get the idea above.
In bash, I could do simply:
sleep 4 & pid1=$! sleep 6 & pid2=$! sleep 8 & pid3=$! wait $pid1 $pid2 $pid3
Thank.
One simple example: using multiprocessing:
import multiprocessing as mp import time pool = mp.Pool(3) results = pool.map(time.sleep, [4, 6, 8] )
, . GIL ( cpython), , python ( cpython ). , , , , ( - threading).
threading
:
threading. - :
import time,threading def mySleep( sec ): time.sleep( sec ) t1 = threading.Thread( target=mySleep, args=(4,) ) t2 = threading.Thread( target=mySleep, args=(6,) ) t3 = threading.Thread( target=mySleep, args=(8,) ) t1.start() t2.start() t3.start() # All threads running in parallel, now we wait t1.join() t2.join() t3.join()
from threading import Thread threads = [Thread(target=time.sleep, args=(secs,)) for secs in (4,6,8)] for t in threads: t.start() for t in threads: t.join() print 'all threads done!'