Skip to content

Instantly share code, notes, and snippets.

@fredrick
Created March 17, 2011 21:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fredrick/875201 to your computer and use it in GitHub Desktop.
Save fredrick/875201 to your computer and use it in GitHub Desktop.
Using Python's multiprocessing module, it's easy to create asynchronous subprocesses
#!/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