'Can I put a Coroutines in while()?
I'm a programming novice, I need this architecture for some reason, but when I do, this method is not feasible, can someone provide some, alternative methods for me to learn? I want to run both asynchronous methods and generic methods
import json
import time
import random
import asyncio
async def post_data():
Yaw=round(random.uniform(-1,1),2)
print(Yaw)
time.sleep(1)
asyncio.run(post_data)
def other_things():
print("COOL")
if __name__ == '__main__':
while True:
post_data()
other_things()
I have try this way, but the structure I really needed had to use asynchronous methods, so it didn't help me.
import json
import time
import random
import asyncio
def post_data():
Yaw=round(random.uniform(-1,1),2)
print(Yaw)
time.sleep(1)
def other_things():
print("COOL")
if __name__ == '__main__':
while True:
post_data()
other_things()
Solution 1:[1]
You can do it like this:
import json
import time
import random
import asyncio
async def post_data():
Yaw=round(random.uniform(-1,1),2)
print(Yaw)
await asyncio.sleep(1)
def other_things():
print("COOL")
async def main():
while True:
await post_data()
other_things()
if __name__ == '__main__':
asyncio.run(main())
Result:
0.78
COOL
0.1
COOL
0.39
COOL
-0.9
...
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 | user56700 |
