python 线程间通信方法一:event 示例

参考了网上的说明,经测试的示例如下:

主要是采用了event的三个方法,即set、clear、wait


#! /usr/bin/python


'''
Created on Apr 24, 2012

@author: stedy
'''


import threading

 

class mythread(threading.Thread):

    def __init__(self,threadname):

        threading.Thread.__init__(self,name=threadname)

    def run(self):

        global event

        if event.isSet():
            print '\nevent: isset\n'

            event.clear()
            
            print'\n\t %s go to wait\n'%self.getName()
            event.wait()
            print '\n\t %s wait over\n' %self.getName()
            
        else:
            print '\n %s event: unset\n' %self.getName()

            event.set()

event=threading.Event()

event.set()

t1=[]

for i in range(10):

    t=mythread(str(i))

    t1.append(t)

 

for i in t1:

    i.start()








你可能感兴趣的:(python)