abck | Library which makes generating akamai cookies | Plugin library
kandi X-RAY | abck Summary
kandi X-RAY | abck Summary
Abck is a library that makes generating akamai cookies easier.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of abck
abck Key Features
abck Examples and Code Snippets
Community Discussions
Trending Discussions on abck
QUESTION
I wanted to combine ProcessPoolExecutor with asyncio to run my blocking functions in TestClass concurrently. Each task is intended to be run long-time, so i need a working shutdown process to make things smooth after exiting my script. Any ideas where do i need to add error handling for KeyboardInterrupt to smoothly shutdown all tasks and processes? I have searched a lot of topics related and none of them solves my issue to work as i wanted. Hope to get some help! Thanks in advance.
...ANSWER
Answered 2021-Aug-29 at 19:31Windows Solution
If you are running on Windows then CTRL-C interrupt handling does not seem to work too well with multiprocessing pools. The following is a bit clumsy but seems to work after a fashion.
The idea is to initialize each process in the multiprocessing pool with a global variable ctrl_c_entered
that is set initially to False
. I have completed your class TestClass
with a method foo
which will be the worker function invoked. It must when invoked:
- Test the global flag
ctrl_c_entered
and if True, immediately return. - Have its own KeyboardInterrupt handler and on such an interrupt it must set the global
ctrl_c_entered
flag to True and return. - Update: However, the CTRL-C could be entered when the pool process has not transferred control to the worker function. For instance, it could be in the process of grabbing from the input queue the next task to run. In this case there would otherwise be no
try/catch
for KeyboardInterrupt exceptions in effect. So we needs to set an interrupt handler for the SIGINT interrupt for each process in the pool that will set thectrl_c_entered
flag toTrue
. But this now means that the original, defaultSIGINT
interrupt handler must be temporarily restored in Step 2 above in order to catch KeyboardInterrupt exceptions.
You also have to let all the submitted asyncio tasks complete. So we set a signal.SIGINT
interrupt handler that sets a global ctrl_c_entered
flags for the main process to True
if CTRL-C has been entered (we do not break out of the asyncio.run(main()
statement. Our long-running asyncio tasks must check this ctrl_c_entered
flag and terminate if it is set to True.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install abck
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page