开发者

execute functions in a queue

开发者 https://www.devze.com 2023-01-08 07:31 出处:网络
i have a example who should show what i\'d like to do queue = 2 def function(): print \'abcd\' 开发者_如何转开发time.sleep(3)

i have a example who should show what i'd like to do

queue = 2

def function():
    print 'abcd'
  开发者_如何转开发  time.sleep(3)

def exec_times(times):
    #do something
    function()

def exec_queue(queue):
    #do something
    function()

exec_times(3)
#things need be working while it waiting for the function finish
time.sleep(10)

the result should be

abcd
abcd

#after finish the first two function executions
abcd

so, there is a way to do that without use thread?

i mean some glib function to do this job.


If you want to avoid threads, one option is to use multiple processes. If you're on python 2.6, take a look at the multiprocessing module. If python 2.5, look at pyprocessing.

Note "Process Pools" in the docs for multiprocessing, which seem to handle your requirements:

One can create a pool of processes which will carry out tasks submitted to it with the Pool class.

class multiprocessing.Pool([processes[, initializer[, initargs[, maxtasksperchild]]]])

A process pool object which controls a pool of worker processes to which jobs can be submitted. It supports asynchronous results with timeouts and callbacks and has a parallel map implementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号