Issue
I intend to run a single asynchronous function (that does things relevant to other parts of the program, but returns nothing) for as long as my synchronous code continues running. Right after I run the asyncio.run()
command, though, the program only performs the asynchronous command forever.
I've tried some elementary threading and multiprocressing Pool approaches, but I found this to be the most successful of what I've attempted:
# There's some code up here (A), and it runs synchronously and fine.
# Here's the asynchronous code (B).
pauseAsynchronousFunction = False
async def asynchronousFunction():
global pauseAsynchronousFunction
# Do stuff before hitting the while loop.
while True:
if pauseAsynchronousFunction == False:
# Do some stuff.
else:
# Do some other stuff.
asyncio.run(pauseAsynchronousFunction())
# More synchronous code is down here (C), which it never reaches.
I can tell that it's not working as I expect because another function below (in C) calls the same global variable pauseAsynchronousFunction
and toggles it from False
to True
, and then back to False
again once it finishes running. That toggle never occurs.
I'm guessing that the problem has either to do with the while True
, which I'm not understanding why if it's asynchronous, or that it's got something to do with not containing an await
statement. What else might I be missing?
UPDATE:
I worked through a few debug attempts with some help from pypypy, and got this.
pauseAsynchronousFunction = False
async def fun2():
global pauseAsynchronousFunction
while True:
if pauseAsynchronousFunction == False:
#do stuff
else:
#do other stuff
async def fun1():
global pauseAsynchronousFunction
pauseAsynchronousFunction = True
#do stuff here that you wanted to do after the `asyncio.run` above
pauseAsynchronousFunction = False
return stuff
async def main():
await asyncio.gather(
fun1(),
fun2(),
)
asyncio.run(main)
The problem appears to be this:
pauseAsynchronousFunction
toggles to True
, but it doesn't toggle back to False
at the end of fun(1)
.
Solution
asyncio.run
will block until your asynchronous functions have all returned. So the code you have after the asyncio.run()
should be converted to an async function i.e.
import asyncio
async def fun2():
global pauseAsynchronousFunction
while True:
if pauseAsynchronousFunction == False:
print('not paused')
else:
print('paused')
await asyncio.sleep(1)
async def fun1():
global pauseAsynchronousFunction
pauseAsynchronousFunction = True
print('im pausing')
await asyncio.sleep(5)
print('im unpausing')
pauseAsynchronousFunction = False
return 0
async def main():
await asyncio.gather(
fun1(),
fun2(),
)
await main()
Here is an alternative answer using threading and Events:
import threading
import time
should_pause = threading.Event()
def fun2():
while True:
if should_pause.is_set():
print('paused')
else:
print('not paused')
time.sleep(1)
t = threading.Thread(target=fun2)
t.start()
should_pause.set()
print('waiting')
time.sleep(5)
print('finished-waiting')
should_pause.clear()
Answered By - pypypy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.