Async
unknown
python
16 days ago
860 B
2
Indexable
Never
import asyncio async def yield_control(): future = asyncio.Future() def callback(): future.set_result(None) # 0 oznacza że oddajemy kontrolę do event loop od razu asyncio.get_event_loop().call_later(0, callback) await future async def task_one(): print("Task One: Started") for i in range(1, 11): print(f"Task One: Running, Item: {i}") if i == 5: await yield_control() print("Task One: Completed") async def task_two(): for i in range(1, 11): print(f"Task Two: Running, Item: {i}") if i == 5: await yield_control() print("Task Two: Completed") async def main(): # Run both tasks concurrently await asyncio.gather(task_one(), task_two()) # Run the event loop with the main coroutine if __name__ == "__main__": asyncio.run(main())
Leave a Comment