Have Python wait for a function to finish before p

2019-02-20 16:47发布

I have a python program that I have written. This python program calls a function within a module I have also written and passes it some data.

program:

def Response(Response):
    Resp = Response
def main():
   myModule.process_this("hello") #Send string to myModule Process_this function
   #Should wait around here for Resp to contain the Response
   print Resp

That function processes it and passes it back as a response to function Response in the main program.

myModule:

def process_this(data)
    #process data
    program.Response(data)

I checked and all the data is being passed correctly. I have left out all the imports and the data processing to keep this question as concise as possible.

I need to find some way of having Python wait for resp to actually contain the response before proceeding with the program. I've been looking threading and using semaphores or using the Queue module, but i'm not 100% sure how I would incorporate either into my program.

1条回答
放我归山
2楼-- · 2019-02-20 17:03

Here's a working solution with queues and the threading module. Note: if your tasks are CPU bound rather than IO bound, you should use multiprocessing instead

import threading
import Queue

def worker(in_q, out_q):
    """ threadsafe worker """
    abort = False
    while not abort:
        try:
            # make sure we don't wait forever
            task = in_q.get(True, .5)
        except Queue.Empty:
            abort = True
        else:
            # process task
            response = task
            # return result 
            out_q.put(response)
            in_q.task_done()
# one queue to pass tasks, one to get results
task_q = Queue.Queue()
result_q = Queue.Queue()
# start threads
t = threading.Thread(target=worker, args=(task_q, result_q))
t.start()
# submit some work
task_q.put("hello")
# wait for results
task_q.join()
print "result", result_q.get()
查看更多
登录 后发表回答