Skip to content

Instantly share code, notes, and snippets.

@kgriffs
Last active July 25, 2022 18:39
Show Gist options
  • Save kgriffs/514cfcd60829eea3d7b1744241ac034e to your computer and use it in GitHub Desktop.
Save kgriffs/514cfcd60829eea3d7b1744241ac034e to your computer and use it in GitHub Desktop.
Using httpx with multiprocessing
import asyncio
import multiprocessing
import httpx
class Publisher:
def __init__(self):
# NOTE(kgriffs): Explicitly manage the client so we can use the
# connection pool. This class could be made into a singleton
# or something to make it easy to reuse across modules without
# having to pass the instance around.
self._client = httpx.AsyncClient()
async def echo(self, number):
r = await self._client.get('https://httpbin.org/anything', headers={'X-Publisher': str(number)})
return r.json()
async def close(self):
await self._client.aclose()
async def main_async(number):
publisher = Publisher()
try:
result = await publisher.echo(number)
print(result)
finally:
# NOTE(kgriffs): Not strictly necessary since the process is
# about to exit and the OS should clean up the resources
# for us.
await publisher.close()
def main(number):
print(f'Starting processor: {number}')
asyncio.run(main_async(number))
if __name__ == "__main__":
number_to_process = range(1, 10)
with multiprocessing.Pool() as pool:
pool.map(main, number_to_process)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment