Question on orderly shutdown of running tasks

19 views
Skip to first unread message

Alan Yorinks

unread,
Jun 10, 2015, 11:13:31 AM6/10/15
to python...@googlegroups.com
I am using Python 3.4.3.

I have an application that uses a task to continuously read data from a serial device. This task is invoked with "loop.run_until_complete". While this task collecting and interpreting data, it may fire off other tasks. In addition, commands are also arriving from method calls from a user API that fire off other tasks with "run_until_complete". The application runs forever until the user hits "Control C". 

Is there a way for me to cleanly exit (no exceptions or errors) by:

1. Getting a list of currently running tasks.
2. Iterate through that list to cancel the tasks.
3. Test that the tasks are cancelled.
4. Exit the program.

Perhaps there are alternatives to accomplish the same thing and I would like to know how to accomplish the above.

If there is, I still would like to know how to get a list of currently running tasks and to cancel those tasks.



Martin Teichmann

unread,
Jun 11, 2015, 4:48:57 AM6/11/15
to python...@googlegroups.com
Hi Alan,


I have an application that uses a task to continuously read data from a serial device. This task is invoked with "loop.run_until_complete". While this task collecting and interpreting data, it may fire off other tasks. In addition, commands are also arriving from method calls from a user API that fire off other tasks with "run_until_complete". The application runs forever until the user hits "Control C". 

Is there a way for me to cleanly exit (no exceptions or errors) by:

1. Getting a list of currently running tasks.
2. Iterate through that list to cancel the tasks.
3. Test that the tasks are cancelled.
4. Exit the program.
 

I asked a very similar question recently, which can be found here:

https://groups.google.com/d/msg/python-tulip/qQbdxREjn1Q/2VeUfxxy2TsJ

Some people gave nice answers to that, maybe it also helps you.

Greetings

Martin

Alan Yorinks

unread,
Jun 11, 2015, 8:03:01 AM6/11/15
to python...@googlegroups.com
Hi Martin,
   Thanks so much for the pointer to that link. That seemed to do the trick.

Alan

Skip Montanaro

unread,
Jun 11, 2015, 8:54:24 AM6/11/15
to python-tulip
I wonder if something akin to daemon threads would be appropriate here
(daemon tasks?). When the last non-daemon task completes, the program
can exit.

Skip

Andrew Svetlov

unread,
Jun 11, 2015, 9:22:51 AM6/11/15
to Skip Montanaro, python-tulip
Daemon task looks like regular task with disabled checks in own
`__del__` method.
--
Thanks,
Andrew Svetlov
Reply all
Reply to author
Forward
0 new messages