'Data race and asyncio python
Trying to understand how asyncio works. The task: have to add some code to lower pseudocode so that requests must be consecutive (0, 1, 2... 9), I need to exclude data race. I tried to add loop (asyncio.get_event_loop() and run_until_complete()), but got only 'RuntimeError: This event loop is already running'. In what direction should I move in order to understand how it works? Thank you!
requests = [server.some_method(value) for value in range(10)]
await asyncio.gather(*requests)
Solution 1:[1]
Code using async/await runs from top-to-bottom exactly like normal code.
If you just want to send 9 requests consecutive then you write code like you normally do - except for adding await:
for value in range(10):
await sever.some_method(value)
asyncio.gather() is for executing something concurrently - the point of async/await. The code you have sent, will execute server.some_method() 9 times in any order (unknown; at the same time).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bluenix |
