I suspect the callbacks are not called because you aren't running Twisted's eventloop (known as the reactor
). Remove your sleep function and replace it with reactor.run()
.
from twisted.internet import reactor
# ...
def main():
a = session.get('https://reqres.in/api/users')
a.addCallbacks(processResponse, processResponse)
#time.sleep(5) # never use blocking functions like this w/ Twisted
reactor.run()
The catch is Twisted's reactor cannot be restarted, so once you stop the event loop (ie. reactor.stop()
), an exception will be raised when reactor.run()
is executed again. In other words, your script/app will only "run once". To circumvent this issue, I suggest you use crochet
. Here's a quick example using a similar example from requests-thread
:
import crochet
crochet.setup()
print('setup')
from twisted.internet.defer import inlineCallbacks
from requests_threads import AsyncSession
session = AsyncSession(n=100)
@crochet.run_in_reactor
@inlineCallbacks
def main(reactor):
responses = []
for i in range(10):
responses.append(session.get('http://httpbin.org/get'))
for response in responses:
r = yield response
print(r)
if __name__ == '__main__':
event = main(None)
event.wait()
And just as an FYI requests-thread
is not for production systems and is subject to significant change (as of Oct 2017). The end goal of this project is to design an awaitable design pattern for requests
in the future. If you need production ready concurrent requests, consider grequests
or treq
.