python thread practices

巴扎黑
发布: 2016-12-09 10:42:44
原创
1482人浏览过

"""" python中使用线程有两种方式:函数或者用类来包装线程对象。 

1、  函数式:调用thread模块中的start_new_thread()函数来产生新线程。线程的结束可以等待线程自然结束,也可以在线程函数中调用thread.exit()或thread.exit_thread()方法。 
import time 
import thread 

def timer(no,interval): 
  cnt=0 
  
  while cnt<10: 
    print 'thread(%d)'%(cnt) 
    time.sleep(interval) 
    cnt=cnt+1 
  

def test(): 
  thread.start_new_thread(timer,(1,1)) 
  thread.start_new_thread(timer,(2,2)) 

if __name__=='__main__': 
  print 'thread starting' 
  test() 
  time.sleep(20) 
  thread.exit_thread() 
  print 'exit...' 
2、  创建threading.thread的子类来包装一个线程对象, 
threading.thread类的使用: 

1,在自己的线程类的__init__里调用threading.thread.__init__(self, name = threadname) 

threadname为线程的名字 

2, run(),通常需要重写,编写代码实现做需要的功能。 

3,getname(),获得线程对象名称 

4,setname(),设置线程对象名称 

5,start(),启动线程 

6,jion([timeout]),等待另一线程结束后再运行。 

7,setdaemon(bool),设置子线程是否随主线程一起结束,必须在start()之前调用。默认为false。 

8,isdaemon(),判断线程是否随主线程一起结束。 

9,isalive(),检查线程是否在运行中。 
import threading 
import time 

class timer(threading.thread): 
  def __init__(self,num,interval): 
    threading.thread.__init__(self) 
    self.thread_num=num 
    self.interval=interval 
    self.thread_stop=false 

  def run(self): 
    while not self.thread_stop: 
      print 'thread object (%d), time %s\n' %(self.thread_num,time.ctime()) 
      time.sleep(self.interval) 

  def stop(self): 
    self.thread_stop=true 

def test(): 
  thread1=timer(1,1) 
  thread2=timer(2,2) 
  thread1.start() 
  thread2.start() 
  time.sleep(10) 
  thread1.stop() 
  thread2.stop() 
  return 

if __name__=='__main__': 
  test() 
""" 
""" 
问题产生的原因就是没有控制多个线程对同一资源的访问,对数据造成破坏,使得线程运行的结果不可预期。这种现象称为“线程不安全”。 
import threading 
import time 

class mythread(threading.thread): 
  def run(self): 
    for i in range(3): 
      time.sleep(1) 
      msg='i am '+ self.getname()+'@'+str(i) 
      print msg 

def test(): 
  for i in range(5): 
    t=mythread() 
    t.start() 

if __name__=='__main__': 
  test() 

上面的例子引出了多线程编程的最常见问题:数据共享。当多个线程都修改某一个共享数据的时候,需要进行同步控制。 

线程同步能够保证多个线程安全访问竞争资源,最简单的同步机制是引入互斥锁。互斥锁为资源引入一个状态:锁定/非锁定。某个线程要更改共享数据时,先将其锁定,此时资源的状态为“锁定”,其他线程不能更改;直到该线程释放资源,将资源的状态变成“非锁定”,其他的线程才能再次锁定该资源。互斥锁保证了每次只有一个线程进行写入操作,从而保证了多线程情况下数据的正确性。其中,锁定方法acquire可以有一个超时时间的可选参数timeout。如果设定了timeout,则在超时后通过返回值可以判断是否得到了锁,从而可以进行一些其他的处理 

threading模块中定义了lock类 
import threading 
import time 

class mythread(threading.thread): 
  def run(self): 
    global num 
    time.sleep(1) 

    if mutex.acquire(): 
      num=num+1 
      print self.name+' set num to '+str(num)+'\n' 
      mutex.release() 
num=0 
mutex=threading.lock() 

def test(): 
  for i in range(5): 
    t=mythread() 
    t.start() 

if __name__=='__main__': 
  test() 
""" 
"""更简单的死锁情况是一个线程“迭代”请求同一个资源,直接就会造成死锁: 
import threading 
import time 

class mythread(threading.thread): 
    def run(self): 
        global num 
        time.sleep(1) 

        if mutex.acquire(1):  
            num = num+1 
            msg = self.name+' set num to '+str(num) 
            print msg 
            mutex.acquire() 
            mutex.release() 
            mutex.release() 
num = 0 
mutex = threading.lock() 
def test(): 
    for i in range(5): 
        t = mythread() 
        t.start() 
if __name__ == '__main__': 
    test() 
为了支持在同一线程中多次请求同一资源,python提供了“可重入锁”:threading.rlock。rlock内部维护着一个lock和一个counter变量,counter记录了acquire的次数,从而使得资源可以被多次require。直到一个线程所有的acquire都被release,其他的线程才能获得资源。上面的例子如果使用rlock代替lock,则不会发生死锁: 
import threading 
import time 

class mythread(threading.thread): 
    def run(self): 
        global num 
        time.sleep(1) 

        if mutex.acquire(1):  
            num = num+1 
            msg = self.name+' set num to '+str(num) 
            print msg 
            mutex.acquire() 
            mutex.release() 
            mutex.release() 
num = 0 
mutex = threading.rlock() 
def test(): 
    for i in range(5): 
        t = mythread() 
        t.start() 
if __name__ == '__main__': 
    test() 
""" 

""" 
python多线程编程(5): 条件变量同步 

互斥锁是最简单的线程同步机制,python提供的condition对象提供了对复杂线程同步问题的支持。condition被称为条件变量,除了提供与lock类似的acquire和release方法外,还提供了wait和notify方法。线程首先acquire一个条件变量,然后判断一些条件。如果条件不满足则wait;如果条件满足,进行一些处理改变条件后,通过notify方法通知其他线程,其他处于wait状态的线程接到通知后会重新判断条件。不断的重复这一过程,从而解决复杂的同步问题。 

可以认为condition对象维护了一个锁(lock/rlock)和一个waiting池。线程通过acquire获得condition对象,当调用wait方法时,线程会释放condition内部的锁并进入blocked状态,同时在waiting池中记录这个线程。当调用notify方法时,condition对象会从waiting池中挑选一个线程,通知其调用acquire方法尝试取到锁。 

condition对象的构造函数可以接受一个lock/rlock对象作为参数,如果没有指定,则condition对象会在内部自行创建一个rlock。 

除了notify方法外,condition对象还提供了notifyall方法,可以通知waiting池中的所有线程尝试acquire内部锁。由于上述机制,处于waiting状态的线程只能通过notify方法唤醒,所以notifyall的作用在于防止有线程永远处于沉默状态。 

演示条件变量同步的经典问题是生产者与消费者问题:假设有一群生产者(producer)和一群消费者(consumer)通过一个市场来交互产品。生产者的”策略“是如果市场上剩余的产品少于1000个,那么就生产100个产品放到市场上;而消费者的”策略“是如果市场上剩余产品的数量多余100个,那么就消费3个产品。用condition解决生产者与消费者问题的代码如下: 
import threading 
import time 

class producer(threading.thread): 
  def run(self): 
    global count 
    while true: 
      if con.acquire(): 
        if count>1000: 
          con.wait() 
        else: 
          count=count+100 
          print self.name+' produce 100,count='+str(count) 

        con.release() 
        time.sleep(1) 

class customer(threading.thread): 
  def run(self): 
    global count 
    while true: 
      if con.acquire(): 
        if count>100: 
          count=count-100 
          print self.name+ 'consume 100, count='+str(count) 
        else: 
          con.wait() 
        con.release() 
        time.sleep(1) 

count=500 
con=threading.condition() 

def test(): 
  for i in range(5): 
    p=producer() 
    p.start() 
    c=customer() 
    c.start() 
    print i 


if __name__=='__main__': 
  test() 
python中默认全局变量在函数中可以读,但是不能写但是 
对con只读,所以不用global引入"""

讯飞智作-讯飞配音
讯飞智作-讯飞配音

讯飞智作是一款集AI配音、虚拟人视频生成、PPT生成视频、虚拟人定制等多功能的AI音视频生产平台。已广泛应用于媒体、教育、短视频等领域。

讯飞智作-讯飞配音 67
查看详情 讯飞智作-讯飞配音
相关标签:
python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号