Python 多线程
In [5]:
from time import sleep, ctime
def loop0():
print 'start loop 0 at: ',ctime()
sleep(4)
print 'loop 0 done at: ', ctime()
def loop1():
print 'start loop 1 at: ', ctime()
sleep(2)
print 'loop 1 done at: ', ctime()
def main():
print 'starting at: ', ctime()
loop0()
loop1()
print 'all Done at:', ctime()
main()
In [4]:
import thread
from time import ctime, sleep
def loop0():
print 'start loop 0 at: ',ctime()
sleep(4)
print 'loop 0 done at: ', ctime()
def loop1():
print 'start loop 1 at: ', ctime()
sleep(2)
print 'loop 1 done at: ', ctime()
def main():
print 'start at: ', ctime()
thread.start_new_thread(loop0, ())
thread.start_new_thread(loop1, ())
sleep(6)
print 'all Done at: ', ctime()
main()
In [9]:
import thread
from time import ctime, sleep
loops = [4, 2]
def loop(nloop, nsec, lock):
print 'start loop', nloop, 'at', ctime()
sleep(nsec)
print 'loop', nloop, 'done at:', ctime()
lock.release()
def main():
print 'starting at: ', ctime()
locks = []
nloops = range(len(loops))
for i in nloops:
lock= thread.allocate_lock()
lock.acquire()
locks.append(lock)
for i in nloops:
thread.start_new_thread(loop,(i, loops[i], locks[i]))
for i in nloops:
while locks[i].locked():
pass
print 'all Done at: ', ctime()
main()
In [10]:
import threading
from time import ctime, sleep
loops = [4, 2]
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()
main()
In [14]:
import threading
from time import ctime, sleep
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()
main()
In [21]:
import threading
from time import ctime, sleep
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.result
def run(self):
print 'starting', self.name, 'at: ', ctime()
self.result = apply(self.func, self.args)
print self.name, 'finished at: ', ctime()
def fib(x):
sleep(0.005)
if x < 2: return x
return fib(x-1) + fib(x-2)
def fac(x):
sleep(0.1)
if x < 2: return x
return fac(x-1) * x
def sum(x):
sleep(0.1)
if x<2: return x
return x+sum(x-1)
funcs = [fib, fac, sum]
n = 12
def main():
nfuncs = range(len(funcs))
print '*** SINGLE THREAD'
for i in nfuncs:
print 'starting', funcs[i].__name__, 'at: ', ctime()
print funcs[i](n)
print funcs[i].__name__, 'finished at: ', ctime()
print '\n *** MULTI THREAD'
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()
print 'all done'
main()
In [24]:
from random import randint
from time import sleep
from Queue import Queue
import threading
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.result
def run(self):
print 'starting', self.name, 'at: ', ctime()
self.result = apply(self.func, self.args)
print self.name, 'finished at: ', ctime()
def writeQ(queue):
print 'producting object for Q...',queue.put('xxx', 1)
print 'size now', queue.qsize()
def readQ(queue):
val = queue.get(1)
print 'consumed object from Q...', 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()
print 'all Done'
main()
In [26]:
import Tkinter
top = Tkinter.Tk()
hello = Tkinter.Label(top, text = 'hello world!')
hello.pack()
quit = Tkinter.Button(top, text='quit', command=top.quit, bg='red', fg='white')
quit.pack(fill = Tkinter.X, expand=1)
Tkinter.mainloop()
In [ ]: