3

I am trying to make an async HTTP request in Python. I use requests - which is very nice - which does not provide async possibilities.

Everytime I want to send an HTTP request, I start a new thread. It executes properly and should returns the response within the main thread so I could execute a callback method.

I am currently using the threading module like this:

class MyConnection
    # ...
    def make_request(self):
        # Use requests lib here
        response = requests.request(...)
        self.receive_response(response)

    def receive_response(self):
        # process response here because it is my callback method

    def start(self):
        thread = threading.Thread(target=self.make_resquest)
        thread.start()

Everything is working fine but methods receive_response should be called within the main thread instead of the thread I launched in start method. How could I go back to the main thread to execute the receive_response method ?

Many thanks for your help !

t00f
  • 563
  • 1
  • 5
  • 15
  • What is the main thread doing in the meantime? – Jeremy West May 28 '14 at 22:21
  • You might also want to see this question: http://stackoverflow.com/questions/18989446/execute-python-function-in-main-thread-from-call-in-dummy-thread – Jeremy West May 28 '14 at 22:23
  • If you are working in a GUI environment with an event loop, you might also look at this: http://stackoverflow.com/questions/270648/tkinter-invoke-event-in-main-loop – Jeremy West May 28 '14 at 22:26
  • The main thread is a serie of python script lines (print & computations) with other independant HTTP requests. When my request is finished, I want my main thread to go back in a callback, execute it and then continue with scripts lines and other independant request – t00f May 28 '14 at 22:54
  • I think your best bet is something like the first question I posted. In a GUI environment, there is already an event queue setup that you can possibly put something into. If your main thread is busy, all you can do is have it check a queue of pending tasks from time to time. Another option is to have the main thread spawn a secondary thread that does all the work and starts all the jobs. Then the main thread just waits to handle responses? – Jeremy West May 29 '14 at 21:08

1 Answers1

0

You could make 'thread' and 'response' member variables of the class and join the thread in 'receive_response'. After you have joined the thread you know that the thread has finished receiving 'response' so it is ready for processing.

IvarTJ
  • 36
  • 2
  • Can you join a secondary thread to the main thread from the secondary thread? It seems like he needs some of kind of invoke functionality? – Jeremy West May 28 '14 at 22:19
  • I imagined that start would dispatch an asynchronous request that could be received in a later call to receive_response. This would enable the main thread to do other things while the request was in progress. The make_request method should in that case not call receive_response. – IvarTJ May 28 '14 at 22:36
  • Jeremy is right : If I use a join call, my main thread will wait until the request is ended. I need something completely async in order to the main thread could continue processing other requests. How would you call the receive_response instead ? – t00f May 28 '14 at 22:40
  • If you would like to wait for multiple threads simultaneously, then I suppose queuing is the right solution. – IvarTJ May 28 '14 at 22:50
  • I am looking for 1 request at the moment. I could have multiple requests but in a separate instance of MyConnection – t00f May 28 '14 at 22:54