Consider the following case: There is a slow server which use about 200ms to handle a request (no including the network transfer time). And now, we need to send a bunch of requests every second.
After read this post, I have tried multi-thread, multi-process, twisted (agent.request) and eventlet. But the biggest speedup is only 6x, which is achieved via twisted and eventlet, both are using epoll.
The following code shows the test version with eventlet,
import eventlet
eventlet.monkey_patch(all=False, socket=True)
import requests
def send():
pile = eventlet.GreenPile(30)
for i in range(1000):
pile.spawn(requests.get, 'https://api.???.com/', timeout=1)
for response in pile:
if response:
print response.elapsed, response.text
Anyone could help me to make it clear why the speedup is so low? And is there any other mechanism could make it much faster?