-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Iteration method stuck in a loop when parent process dies #325
Comments
Hi there, thanks for opening this issue! The behavior you describe ( Nonetheless, your use case sounds reasonable, so let's see how we can solve it 🙂 I'd suggest using a Let's see an example: import asyncio
import aiomqtt
async def child():
print("Child task running")
try:
async with aiomqtt.Client("test.mosquitto.org") as client:
await client.subscribe("temperature/#")
async for message in client.messages:
print(message.payload)
except asyncio.CancelledError:
print("Child task cancelled")
async def parent():
print("Parent task running")
async with asyncio.TaskGroup() as tg:
tg.create_task(child())
await asyncio.sleep(1)
print("Raising exception in parent task")
raise Exception
asyncio.run(parent()) Let me know if that helps! 🙂 |
Thank you for your suggestion.
If we fix the code with the code I suggested earlier, there is no more need of the while in the killer task. |
Thanks for reporting back! 👍 You're right that we should really avoid the If you want to cancel the client manually inside the parent, you can do something like this: import asyncio
import aiomqtt
async def child():
print("Child task running")
try:
async with aiomqtt.Client("test.mosquitto.org") as client:
await client.subscribe("temperature/#")
async for message in client.messages:
print(message.payload)
except asyncio.CancelledError:
print("Child task cancelled")
async def parent():
print("Parent task running")
async with asyncio.TaskGroup() as tg:
task = tg.create_task(child())
await asyncio.sleep(1)
# Do something that might raise an exception on which the child should be prematurely canceled
if False:
print("Raising exception in parent task")
raise Exception
# Cancel the child task manually
print("Canceling the child manually")
task.cancel()
# Do other things
print("Doing other things inside the parent")
await asyncio.sleep(2)
asyncio.run(parent()) Let me know if that helps 🙂 |
Hello,
I refer to issue #298.
As it happens to the guy who opened it, the script execution gets stuck inside the for loop that flows the messages from the mqtt broker.
I read in the code of the updated library, in the anext() function that it is unblocked only for:
-disconnecting the broker
-iterating over the next arrived message
However, when the thread/task is a child of another process that dies, its execution gets stuck inside the function and so there is no way to kill the child as well.
I tried the given solution but it overloads the CPU.
To solve this problem I would propose to use the function threading.Event.variable_name.is_set() which checks the state of an asserted flag of the parent process in case it dies.
This check should be put inside the function that iterates anext() messages, so as to interrupt execution.
Are there better solutions?
Translated with DeepL.com (free version)
The text was updated successfully, but these errors were encountered: