python多线程编程

python多线程编程架构

import Queue
import threading
import subprocess

q = Queue.Queue()
for i in range(30): #put 30 tasks in the queue
    q.put(i)

def worker():  #replace worker with you want to parallel preprocess
    while True:
        item = q.get()
        #execute a task: call a shell program and wait until it completes
        subprocess.call("echo "+str(item), shell=True) 
        q.task_done()

cpus=numofyourcpu #detect number of cores
print("Creating %d threads" % cpus)
for i in range(cpus):
     t = threading.Thread(target=worker)
     t.daemon = True
     t.start()

q.join() #block until all tasks are done

reference
my demo

posted @ 2017-03-06 20:37  fariver  阅读(168)  评论(0编辑  收藏  举报