Skip to content

Instantly share code, notes, and snippets.

@cloudaice
Created February 7, 2012 08:48
Show Gist options
  • Save cloudaice/1758343 to your computer and use it in GitHub Desktop.
Save cloudaice/1758343 to your computer and use it in GitHub Desktop.
python multi-thread
# -*- coding: utf-8 -*-
from myThread import MyThread
from time import ctime
from time import sleep
import sys
def fib(x):
# sleep(0.005)
if x<2:return 1
return (fib(x-2) + fib(x-1))
def fac(x):
# sleep(0.05)
if x<2:return 1
return (fac(x-2)+fac(x-1))
def sum(x):
# sleep(0.05)
if x<2:return 1
return (sum(x-2)+sum(x-1))
funcs = [fib,fac,sum]
n = 35
def main():
nfuncs = range(len(funcs))
sys.stdout.write('***SINGLE THREAD\n')
for i in nfuncs:
sys.stdout.write('starting %s at: %s\n' %(funcs[i].__name__,ctime()))
print funcs[i](n)
sys.stdout.write("%s finished at: %s\n"% (funcs[i].__name__,ctime()))
sys.stdout.write('\n***MULTIPLE THREADS\n')
threads = []
for i in nfuncs:
t = MyThread(funcs[i],(n,),funcs[i].__name__)
threads.append(t)
for i in nfuncs:
threads[i].start()
for i in nfuncs:
threads[i].join()
print threads[i].getResult()
sys.stdout.write('all done\n')
if __name__ == '__main__':
main()
import threading
from time import ctime
import sys
class MyThread(threading.Thread):
def __init__(self,func,args,name=''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
def getResult(self):
return self.res
def run(self):
sys.stdout.write("starting %s at: %s\n" % (self.name,ctime()))
self.res = apply(self.func,self.args)
sys.stdout.write("%s finished at: %s\n" % (self.name,ctime()))
# -*- coding: utf-8 -*-
'''
Created on 2012-1-24
filename: py_th
@author: cloudaice
'''
import threading
from time import sleep,ctime
loops=[2,4]
def loop(nloop,nsec):
print "start loop",nloop,"at:",ctime()
sleep(nsec)
print "loop",nloop,"done at:",ctime()
def main():
print "starting at:",ctime()
threads = []
nloops=range(len(loops))
for i in nloops:
t = threading.Thread(target=loop,args=(i,loops[i]))
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print "all done at:",ctime()
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
import threading
from time import sleep
from time import ctime
loops=[4,2]
class ThreadFunc(object):
def __init__(self,func,args,name=''):
self.name = name
self.func = func
self.args = args
def __call__(self):
apply(self.func,self.args)
def loop(nloop,nsec):
print 'start loop',nloop,'at:',ctime()
sleep(nsec)
print 'loop',nloop,'done at:',ctime()
def main():
print 'starting at:',ctime()
threads = []
nloops = range(len(loops))
for i in nloops:
t = threading.Thread(target = ThreadFunc(loop,(i,loops[i]),loop.__name__))
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print 'all DONE at:',ctime()
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
import threading
from time import sleep
from time import ctime
loops=[4,2]
class MyThread(threading.Thread):
def __init__(self,func,args,name=''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
def run(self):
apply(self.func,self.args)
def loop(nloop,nsec):
print 'start loop', nloop,'at:',ctime()
sleep(nsec)
print 'loop',nloop,'done at:',ctime()
def main():
print 'starting at:',ctime()
threads = []
nloops = range(len(loops))
for i in nloops:
t = MyThread(loop,(i,loops[i]),loop.__name__)
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print "all done at:",ctime()
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
from random import randint
from time import sleep
from Queue import Queue
from myThread import MyThread
import sys
def writeQ(queue):
sys.stdout.write("producing object for Q ...")
queue.put('xxx',1)
sys.stdout.write("size now %s\n" % queue.qsize())
def readQ(queue):
val = queue.get(1)
sys.stdout.write("consumed object from Q... size now %s\n" % queue.qsize())
def writer(queue,loops):
for i in range(loops):
writeQ(queue)
sleep(randint(1,3))
def reader(queue,loops):
for i in range(loops):
readQ(queue)
sleep(randint(2,5))
funcs = [writer,reader]
nfuncs = range(len(funcs))
def main():
nloops = randint(2,5)
q = Queue(32)
threads = []
for i in nfuncs:
t = MyThread(funcs[i],(q,nloops),funcs[i].__name__)
threads.append(t)
for i in nfuncs:
threads[i].start()
for i in nfuncs:
threads[i].join()
sys.stdout.write('all done\n')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment