1

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.

Brewer
  • 391
  • 1
  • 4
  • 13
  • Are you aware of the fact that the flow of the program will first pass through `myModule.process_this("hello")`, and nothing else will be called or processed until `process_this` will have terminated? What's your problem? – nbro Feb 01 '15 at 00:50
  • I'm fairly certain it isnt flowing like that, because whenever I try to do anything with Resp, it says it isnt defined. I've tried making it a global and changing it's scope, but it still errors and says it isnt defined. So, I figured it must be because it's trying to use the variable before it has been assigned in the other module. – Brewer Feb 01 '15 at 01:17
  • The process_this function in myModule does have functions which it runs, which process the data I pass to it. So, perhaps it's because that process_this terminates and my program is still trying to access the variable before the relative functions in myModule assign a value to it – Brewer Feb 01 '15 at 01:20
  • Try to insert `print`s or `input`s to see the flow of the program (I usually debug it like that when simply using the `IDLE`) – nbro Feb 01 '15 at 02:31

1 Answers1

1

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()
miraculixx
  • 10,034
  • 2
  • 41
  • 60