Asynchronous wrapper for HTTP requests.
pip install tempo-async
import tempo
url = 'https://via.placeholder.com'
num_reqs = 100
requests = [ # map in any request parameters: url, query params, HTTP method, etc.
tempo.RequestConfig(url=url) for _ in range(num_reqs)
]
if __name__ == '__main__':
tempo.run(requests, rate=10) # fetch 100 cat pictures, 10 a second
Take it a step further by collecting the responses,
import tempo
url = 'https://via.placeholder.com'
num_reqs = 100
### cat picture bucket
catpics = []
###
requests = [ # map in any request parameters: url, query params, HTTP method, etc.
tempo.RequestConfig(url=url) for _ in range(num_reqs)
]
if __name__ == '__main__':
tempo.run(requests, collector=catpics, rate=10) # indicated `catpics` should store response
print(f'Pictures stored in list: {len(catpics)}')
… and adding any number of processing functions to handle responses.
import tempo
url = 'https://via.placeholder.com'
num_reqs = 100
catpics = []
requests = [ # map in any request parameters: url, query params, HTTP method, etc.
tempo.RequestConfig(url=url) for _ in range(num_reqs)
]
### processors
def say_hi(res) -> None:
# returns None so does not affect final processed response sent to collectors
print('Hello cat!')
def get_body(res) -> str:
# since it returns a value, this processor changes the final output of `tempo.run`
body = res.text
return body
###
if __name__ == '__main__':
# process the requests in order of listed processors
tempo.run(requests, collector=catpics, rate=10, processors=[say_hi, get_body])
# processors' return values affect output sent to collectors
print(f'Type of stored result: {type(catpics[0])}') # str, not Response object
Submit a pull request! Contributions are welcome!
Please write test coverage for your changes and run tox
to test for backwards compatibility among the supported Python versions.
Open an issue, create a branch, and submit a PR. (Tests for everything!)
run
function.run
function.collector
argument to be an iterable, a function passed a response / processed response object, or a file out. Maybe even stdout.