tags:

views:

67

answers:

1

hi! 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 trhead?

i mean some glib function to do this job.

+2  A: 

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.

ars
i am thinking something like the glib.timeout_add(interval, callback, ...) instead execute function every interval, it will keep watching the function until it finish ,then callback is called.thanks for reply
killown
Take a look at Process Pools -- see the update in my answer.
ars