Три способа выполнить множество задач с 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
🪐 In the galaxy cluster MACS J1206.2-0847, astronomers have mapped dramatic distortions of space-time caused by the cluster's enormous mass. This real effect, called gravitational lensing, bends and magnifies the light from even more distant galaxies behind it, turning MACS J1206.2-0847 into a cosmic lens that reveals objects otherwise hidden from our view. ✨
#spacetime⚡#mysteries⚡#lensing⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space
👉subscribe Universe Mysteries
🪐 The James Webb Space Telescope has revealed the most detailed images yet of the galaxy cluster SMACS 0723, showing thousands of galaxies and ancient starlight that traveled over 4.6 billion years to reach us. Webb’s infrared vision lets us peer through dust and see cosmic structures, such as warped arcs of light caused by gravitational lensing—where the cluster’s mass bends and magnifies even more distant galaxies hiding behind it. ✨
#galaxies⚡#infrared⚡#lensing⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space
👉subscribe Universe Mysteries
🪐 In 2023, astronomers used the gravity of the massive galaxy cluster SMACS J0723.3–7327 to create a powerful "gravitational lens," sharply magnifying and stretching light from galaxies over 13 billion light-years away. This natural lens effect, where space-time bends around clusters and distorts the background like a cosmic funhouse mirror, provides one of the clearest real-world examples of how gravity can twist the fabric of the universe and reveal objects otherwise hidden from view. ✨
#wormholes⚡#spacetime⚡#lensing⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space
👉subscribe Universe Mysteries
👉more Channels
🪐 In 2015, astronomers used data from the Atacama Large Millimeter/submillimeter Array (ALMA) to observe the gravitationally warped light from a distant galaxy called SDP.81. The galaxy's light was stretched and bent into a near-perfect ring by the immense mass of a foreground galaxy, creating a striking "Einstein ring"—a vivid demonstration of how gravity can distort space-time and transform a distant galaxy into a cosmic lens. ✨
#wormholes⚡#spacetime⚡#lensing⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space
👉subscribe Universe Mysteries
👉more Channels
🪐 In the galaxy cluster SDSS J1004+4112, astronomers have observed the light from a single supernova appear in multiple places at different times due to a powerful space-time distortion called gravitational lensing. This real effect means the cluster's massive gravity bends and splits the supernova's light into separate images, allowing telescopes to witness the same stellar explosion replayed across the sky—a cosmic demonstration of how gravity can twist the very fabric of space and time. ✨
#wormholes⚡#spacetime⚡#lensing⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space
👉subscribe Universe Mysteries
🪐 The massive galaxy cluster Abell 370, situated about 4 billion light-years away in the constellation Cetus, acts as a colossal cosmic lens. Its immense gravity bends and magnifies the light from galaxies far beyond, creating spectacular arcs and distorted images that unveil some of the most distant and faint structures ever seen in the universe. ✨
#lensing⚡#galaxies⚡#cluster⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space
👉subscribe Universe Mysteries
🪐 In the galaxy cluster El Gordo, astronomers have observed evidence that most of its mass is not from visible matter but from dark matter—a mysterious, invisible substance that outweighs all the galaxies and gas combined. Detailed gravitational lensing (where gravity bends and magnifies the light from more distant galaxies) reveals vast, hidden regions in El Gordo, confirming that dark matter acts as a cosmic glue, binding this colossal cluster together and shaping its structure on a grand scale. ✨
#darkmatter⚡#lensing⚡#galaxycluster⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space
👉subscribe Universe Mysteries
🪐 In the galaxy cluster Abell 1689, astronomers have observed light from distant galaxies being bent and stretched in strange ways—a real effect called "gravitational lensing." This phenomenon is caused when a huge mass, like the dense collection of galaxies in Abell 1689, distorts space-time itself—warping the paths of light and creating the illusion of arcs, rings, or even multiple images of the same object in the sky. ✨
#spacetime⚡#lensing⚡#clusters⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space
👉subscribe Universe Mysteries
🪐 In the galaxy cluster Abell 1689, scientists have mapped how dark matter bends light from even more distant galaxies in a phenomenon called gravitational lensing. This cosmic "magnifying glass" effect lets astronomers trace the otherwise invisible dark matter—an unknown substance that reveals itself only through gravity, helping piece together how vast amounts of hidden mass shape entire clusters across the universe. ✨
#darkmatter⚡#abell1689⚡#lensing⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space
👉subscribe Universe Mysteries
👉more Channels
🪐 Near the galaxy cluster Abell 370, astronomers have discovered evidence that space-time itself can be “distorted” on a huge scale, bending and stretching the paths of light from galaxies lying far behind the cluster. This real phenomenon, called gravitational lensing, reveals how massive objects can warp the very fabric of the universe and lets scientists study galaxies otherwise hidden from view—showing the true power of space-time distortions in action. ✨
#spacetime⚡#gravitational⚡#lensing⚡#abell370⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space
👉subscribe Universe Mysteries