RequestPool Class
Iury O. G. Figueiredo edited this page Dec 5, 2020
·
2 revisions
The RequestPool class is used to take an action when a given set of requests is finished. It is also an event emitter.
from websnake import Get, ResponseHandle, core, RequestPool, die
def handle_done(request, response):
print('Headers:', response.headers)
print('Code:', response.code)
print('Version:', response.version)
print('Reason:', response.reason)
def handle_empty(pool):
print('All requests done!')
die('Stopping...')
if __name__ == '__main__':
urls = ('https://en.wikipedia.org/wiki/Leonhard_Euler',
'https://www.google.com.br','https://facebook.com/')
pool = RequestPool()
pool.add_map(RequestPool.EMPTY, handle_empty)
for ind in urls:
Get(ind, pool=pool).add_map(ResponseHandle.DONE, handle_done)
core.gear.mainloop()
In the above example when the event RequestPool.EMPTY is fired it gets handle_empty handle called. The RequestPool instance is sent as argument to the handle_empty function.
The line below is responsible for binding the handle_done function to the event ResponseHandle.DONE and also specifying such a request belongs to a pool.
Get(ind, pool=pool).add_map(ResponseHandle.DONE, handle_done)
All the class requests accept a pool argument in its constructor.