您的位置:首页 > 编程语言 > Python开发

python多线程学习

2013-06-20 22:59 393 查看
看得别人的,先把网址记录下来。

还是看代码

#--*--encoding:utf-8--*--
import time                 # 导入时间模块
import threading as thread  # 导入线程模块

class Thread1(thread.Thread):
def __init__(self):
thread.Thread.__init__(self)    # 默认初始化
self.lock = thread.RLock()      # 创建资源锁
self.flag = True
self.count = 0
def run(self):
print 'Thread1 run'
while self.count < 3:
self.lock.acquire()         # 锁住资源
self.count += 1
print self.count            # 输出计数
self.lock.release()         # 释放资源
time.sleep(1)               # 线程休眠1秒
print 'Thread1 end'

class Thread2(thread.Thread):
def __init__(self,event):
thread.Thread.__init__(self)    # 初始化线程
self.event = event
def run(self):
self.event.wait()               # 线程启动后等待事件
print 'Thread2 run'
self.event.clear()              # 清除事件
print 'Thread2 end'

print 'program start'
event = thread.Event()
t1 = Thread1()
t2 = Thread2(event)
t1.start()              # 线程t1启动
t2.start()              # 线程t2启动
t1.join()               # 等待线程t1结束
event.set()             # 激发事件t2开始运行
t2.join()               # 等待线程t2结束
print 'program end'     # 结束程序


# encoding:UTF-8
import threading
import time

event = threading.Event()

def func():
print '%s wait for event' %threading.currentThread().getName()
event.wait()
print '% recv event'%threading.currentThread().getName()

t1 = threading.Thread(target=func)
t2 = threading.Thread(target=func)
t1.start()
t2.start()

time.sleep(10)
print "*"*40
event.set()


# -*- coding: utf-8 -*-
#---- Event
#---- 捉迷藏的游戏
import threading, time
import sys
type = sys.getfilesystemencoding()
print type
class Hider(threading.Thread):
def __init__(self, cond, name):
super(Hider, self).__init__()
self.cond = cond
self.name = name

def run(self):
time.sleep(1) #确保先运行Seeker中的方法

f1 = self.name + ': 我已经把眼睛蒙上了'
print f1.decode('UTF-8').encode(type) , time.ctime()

self.cond.set()

time.sleep(1)

self.cond.wait()
f2 =  self.name + ': 我找到你了 ~_~'
print f2.decode('UTF-8').encode(type) , time.ctime()

self.cond.set()

f3 = self.name + ': 我赢了'
print f3.decode('UTF-8').encode(type) , time.ctime()

class Seeker(threading.Thread):
def __init__(self, cond, name):
super(Seeker, self).__init__()
self.cond = cond
self.name = name
def run(self):
self.cond.wait()

f4 = self.name + ': 我已经藏好了,你快来找我吧'
print f4.decode('UTF-8').encode(type) , time.ctime()
self.cond.set()

time.sleep(1)
self.cond.wait()

f5 = self.name + ': 被你找到了,哎~~~'
print f5.decode('UTF-8').encode(type) , time.ctime()

cond = threading.Event()
seeker = Seeker(cond, 'seeker')
hider = Hider(cond, 'hider')
seeker.start()
hider.start()




import threading
import time

class TestThread(threading.Thread):
def __init__(self, name, event):
super(TestThread, self).__init__()
self.name = name
self.event = event

def run(self):
print 'Thread: ', self.name, ' start at:', time.ctime(time.time())
self.event.wait()
print 'Thread: ', self.name, ' finish at:', time.ctime(time.time())

def main():
event = threading.Event()
threads = []
for i in range(1, 5):
threads.append(TestThread(str(i), event))
print 'main thread start at: ', time.ctime()
event.clear()
for thread in threads:
thread.start()
print 'sleep 5 seconds.......'
time.sleep(5)
print 'now awake other threads....'
event.set()

main()


比较详细的线程介绍

http://www.cnblogs.com/hbycool/articles/2749975.html

http://www.17jo.com/program/python/app/ThreadUse.html

http://blog.sina.com.cn/s/blog_5e2642f40100vyuy.html

http://www.360doc.com/content/12/0425/15/9537713_206437179.shtml

http://cppblog.com/riverbird/archive/2007/12/26/39704.html

英文资料
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: