SHOW:
|
|
- or go back to the newest paste.
| 1 | from multiprocessing import Queue, Process, Pool | |
| 2 | import random | |
| 3 | import time | |
| 4 | ||
| 5 | parameters=range(10) | |
| 6 | - | q=Queue() |
| 6 | + | |
| 7 | ||
| 8 | def calculate(par): | |
| 9 | # Do stuff | |
| 10 | - | q.put(result) |
| 10 | + | time.sleep(5*random.random()) # Randomize writting times. |
| 11 | savingfile.write(result) | |
| 12 | print result | |
| 13 | ||
| 14 | - | def saving(): |
| 14 | + | |
| 15 | - | while True: |
| 15 | + | |
| 16 | - | try: |
| 16 | + | |
| 17 | - | item=q.get(False) |
| 17 | + | |
| 18 | - | except : |
| 18 | + | |
| 19 | - | time.sleep(1) |
| 19 | + | |
| 20 | - | continue |
| 20 | + | |
| 21 | pool.map_async(calculate, parameters, chunksize=1) | |
| 22 | - | savefile.write(result) |
| 22 | + | |
| 23 | - | |
| 23 | + | |
| 24 | ||
| 25 | print 'End!' |