I have the following client code that I borrowed from twisted's docs:
https://docs.twistedmatrix.com/en/twisted-20.3.0/web/howto/client.html#the-agent
And I am trying to run it with asyncio since I am building an asyncio project that requires compatibility with twisted. Here is the code:
import asyncio
from twisted.internet import asyncioreactor
from twisted.web.client import Agent
from twisted.web.http_headers import Headers
asyncioreactor.install()
async def request():
agent = Agent(asyncioreactor.AsyncioSelectorReactor)
d = agent.request(
b'GET',
b'http://httpbin.com/anything',
Headers({'User-Agent': ['Twisted Web Client Example']}),
None)
def cbResponse(ignored):
print('Response received')
d.addCallback(cbResponse)
def cbShutdown(ignored):
asyncioreactor.AsyncioSelectorReactor.stop()
d.addBoth(cbShutdown)
print("This is where it always get stuck")
res = await d.asFuture(asyncio.get_event_loop())
print("SUCCESSS!!!!")
if __name__ == "__main__":
asyncio.run(request())
I saved this as a request.py
file and run it with python request.py
, but it always hangs when reaching this line:
print("This is where it always get stuck")
res = await d.asFuture(asyncio.get_event_loop())
Is it possible to run this with asyncio? I am not too familiar with twisted and my ultimate goal is to be able to run a twisted client with asyncio.