Skip to content

Instantly share code, notes, and snippets.

@koaning
Created April 1, 2018 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koaning/0462d903017add093ded7460566ec46e to your computer and use it in GitHub Desktop.
Save koaning/0462d903017add093ded7460566ec46e to your computer and use it in GitHub Desktop.
async def fetch(url, json_body, session):
async with session.post(url, json=json_body) as response:
return await response.read()
async def run(json_bodies, n_sim=1000):
tasks = []
url = make_url(n_sim=n_sim)
# Fetch all responses within one Client session,
# keep connection alive for all requests.
async with ClientSession(connector=TCPConnector(limit=None), read_timeout=60000, conn_timeout=60000) as session:
for i, body in enumerate(json_bodies):
task = asyncio.ensure_future(fetch(url=url, json_body=body, session=session))
tasks.append(task)
responses = await asyncio.gather(*tasks)
# you now have all response bodies in this variable
return responses
def random_population(n_population=10, n_sim=1000):
init_order = ["maki-1", "maki-2", "maki-3", "sashimi",
"egg", "salmon", "squid", "wasabi", "pudding",
"tempura", "dumpling", "tofu", "eel", "temaki"]
json_bodies = []
for i in range(n_population):
random.shuffle(init_order)
json_bodies.append({"order": init_order})
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run(json_bodies=json_bodies, n_sim=n_sim))
results = loop.run_until_complete(future)
loop.close()
print(f"i just parsed {len(json_bodies)} sequences")
return [(results[i], json_bodies[i]) for i,c in enumerate(json_bodies)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment