Created
August 1, 2013 16:37
-
-
Save chadrik/6133043 to your computer and use it in GitHub Desktop.
testing gist gem
This file contains hidden or 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
print "top foo" | |
f = open("/Volumes/newhome/chad/python/multiproc.py", 'r') | |
exec f |
This file contains hidden or 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
import sys | |
#import cPickle as pickle | |
# from pickle import Pickler | |
# print Pickler.__module__ | |
# print sys.modules['pickle'] | |
# print hasattr(Pickler, 'dispatch') | |
# import imp | |
# print imp.find_module('pickle') | |
# print sys.modules['pickle'] | |
# import pickle | |
# print sys.modules['pickle'] | |
#print sys.modules['cPickle'] | |
#print hasattr(cPickle.Pickler, 'dispatch') | |
#print hasattr(pickle.Pickler, 'dispatch') | |
import multiprocessing | |
import time | |
def create(x): | |
print "here", x | |
try: | |
time.sleep(10) | |
except KeyboardInterrupt: | |
return | |
return x*x | |
def main(): | |
pool = multiprocessing.Pool(2) | |
results = pool.imap(create, [1,2,3,4]) | |
try: | |
while 1: | |
# timeout seconds per job | |
results.next(timeout=12) | |
except StopIteration: | |
pool.close() | |
pool.join() | |
except KeyboardInterrupt: | |
# this never gets called | |
print "keyboard interrupt received" | |
pool.terminate() | |
if __name__ == "__main__": | |
main() | |
# def f(x): | |
# result = x*x | |
# print "result", os.getpid(), result | |
# return result | |
# | |
# class Foo(object): | |
# def doit(self): | |
# pool = Pool(processes=4) # start 4 worker processes | |
# | |
# result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously | |
# print result.get(timeout=1) # prints "100" unless your computer is *very* slow | |
# | |
# print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]" | |
# | |
# it = pool.imap(f, range(10)) | |
# print it.next() # prints "0" | |
# print it.next() # prints "1" | |
# print it.next(timeout=1) # prints "4" unless your computer is *very* slow | |
# | |
# import time | |
# result = pool.apply_async(time.sleep, (10,)) | |
# print result.get(timeout=1) # raises TimeoutError | |
# | |
# print "name is", __name__ | |
# if __name__ in ['__main__','foo']: | |
# Foo().doit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
will this apply to the selected line?