Три способа выполнить множество задач с asyncio
Функция для примера:
async def do_it(n):
await asyncio.sleep(random.uniform(0.5, 1))
return n
1. Последовательный вызов
async def main():
for i in range(100):
result = await do_it(i)
Такой вызов имеет смысл только тогда, когда результат одной задачи требуется для вызова следующей.
Если они независимы, то это антипаттерн, так как аналогичен простому синхронному вызову по очереди.
2. Упорядоченный результат
async def main():
tasks = [do_it(i) for i in range(100)]
results = await asyncio.gather(*tasks)
Выполняет корутины конкурентно и возвращает результат в виде списка.
Полезен когда требуется получить результаты в том же порядке в котором задачи отправлены.
3. Результат по мере готовности
tasks = [asyncio.create_task(do_it(i)) for i in range(100)]
for cor in asyncio.as_completed(tasks):
result = await cor
Так же выполняет корутины конкурентно, но не гарантирует порядок. Результат возвращается по мере готовности, каждый отдельно.
Полезен когда нужно обработать любой ответ как можно скорее.
#async
📰 Wine 11.8 Brings Mono Engine Update and 22 Bug Fixes
Wine 11.8 updates Mono to version 11.1, improves MSXML and VBScript support, and fixes 22 bugs across games and Windows apps.
🔗 Source: https://linuxiac.com/wine-11-8-brings-mono-engine-update-and-22-bug-fixes/
#wine
Ancient factory exposes secrets of winemaking
A 1,500-year-old wine factory the size of a modern-day football field has been unearthed in Israel, showing how vintners met demand for high quality white wine popular throughout the ancient world.
#News#Reuters#Israel#Wine
Subscribe: http://smarturl.it/reuterssubscribe
Reuters brings you the latest business, finance and breaking news video from around the globe. Our reputation for accuracy and impartiality is unparalleled.
Get the latest news on: http://reuters.com/
Follow Reuters on Facebook: https://www.facebook.com/Reuters
Follow Reuters on Twitter: https://twitter.com/Reuters
Follow Reuters on Instagram: https://www.instagram.com/reuters/?hl=en
➖@reutersworldchannel➖
📰 Proton 11.0 Beta 2 Updates VKD3D-Proton
Following the release of Proton 11.0 Beta 1 from two weeks ago that updated against Wine 11.0, this heart to Valve's Steam Play is now out with a second beta release...
🔗 Source: https://www.phoronix.com/news/Proton-11.0-Beta-2
#proton#steam#wine
Early Roman women were forbidden to drink wine, and a husband who found his wife drinking was at liberty to kill her. The prohibition stemmed from a deeply patriarchal society that viewed women as weak and susceptible to vice, believing that wine could lead them to "inappropriate behavior" or "debauchery".
@googlefactss#alcohol#wine#history#nowyouknow