-
-
Save stungkit/bbedcf823928b4040d1e30252ff7042e to your computer and use it in GitHub Desktop.
Using Python's multiprocessing module, it's easy to create asynchronous subprocesses
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
#!/usr/bin/env python | |
# encoding: utf-8 | |
import random | |
import time | |
import multiprocessing | |
if __name__ == '__main__': | |
"""Demonstration of GIL-friendly asynchronous development with Python's multiprocessing module""" | |
def process(instance): | |
total_time = random.uniform(0, 2) | |
time.sleep(total_time) | |
print('Process %s : completed in %s sec' % (instance, total_time)) | |
for i in range(10): | |
"""Create 10 non-blocking subprocesses that will have random runtimes""" | |
multiprocessing.Process(target=process, args=(i,)).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment