Issue
I want to continue my outer loop but in async function it looks like, when the task completed program will stop. this is piece of my code I want to do something in main every 50 seconds and then call blabla
async def blabla():
await client.send_message()
if __name__ == "__main__":
while True:
scrap_time = datetime.now()
print(scrap_time.strftime("%H:%M:%S"), "scraping started ...")
asyncio.run(blabla())
time.sleep(50)
And finally this will be shown in console
Process finished with exit code -1
Solution
I'm not sure exactly what you want to achieve here since your code seems to perform the desired functionality you described of "I want to do something in main every 50 seconds and then call blabla" albeit in a slightly odd manner.
I've refactored this to do exactly what you described of "do something in main every 50 seconds and then call blabla". The sleep is asynchronous so you do not block the main python process with the blocking time.sleep()
call.
import asyncio
from datetime import datetime
async def blabla():
await client.send_message()
async def main():
print("doing something in main...")
scrap_time = datetime.now()
print(scrap_time.strftime("%H:%M:%S"), "scraping started ...")
async def run():
"""
every 50 seconds, do something in main() and then call blabla()
"""
while True:
await main()
await blabla()
await asyncio.sleep(50)
if __name__ == "__main__":
asyncio.run(run())
Functionally this is equivalent to your program but, as I said, from your description it seems that your program already achieves the desired outcome. When debugging you may want to decrease the sleep value from 50 seconds to 1 second so you don't have to wait so long for the rest of the program to resume.
Answered By - laker93
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.