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 !