Created
December 17, 2015 18:24
-
-
Save jacobcase/37d70be85b185fea15ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from concurrent.futures import ProcessPoolExecutor | |
import asyncio | |
import signal | |
import sys | |
def worker(wid): | |
def handler(signum, frame): | |
print("Exit signal in worker {}".format(wid)) | |
signal.signal(signal.SIGINT, handler) | |
signal.signal(signal.SIGTERM, handler) | |
signal.pause() | |
async def run_workers(count): | |
loop = asyncio.get_event_loop() | |
futures = [] | |
print("Starting work") | |
for i in range(count): | |
fut = loop.run_in_executor(None, worker, i) | |
futures.append(fut) | |
print("All work submitted") | |
await asyncio.wait(futures) | |
if __name__ == '__main__': | |
executor = ProcessPoolExecutor(max_workers=10) | |
loop = asyncio.get_event_loop() | |
loop.set_default_executor(executor) | |
def handler(): | |
executor.shutdown() | |
loop = asyncio.get_event_loop() | |
print("main exit signal recieved") | |
# loop.stop() | |
loop.add_signal_handler(signal.SIGINT, handler) | |
print("Running until complete") | |
loop.run_until_complete(asyncio.ensure_future(run_workers(10))) | |
print("Running completed") | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment