我有一个例子,应该展示我想做的事情
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)
结果应该是
abcd
abcd
#after finish the first two function executions
abcd
那么,有没有一种方法可以在不使用线程的情况下做到这一点?
我的意思是一些glib函数来完成这项工作。
最佳答案
如果您想避免线程,一种选择是使用多个进程。如果您使用的是 python 2.6,请查看 multiprocessing模块。如果是 python 2.5,请查看 pyprocessing .
注意多处理文档中的“Process Pools ”,它似乎可以满足您的要求:
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.
关于python - 执行队列中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3276595/