吉吉于

【Python】thread解析

没用过thread,之前一直用threading,最近看green let,geventlet,有个评测涉及到thread,翻了一下,copy别人的文章。

一,在import thread之后,我们help(thread)一下,可以看到如下信息:
  1. NAME thread
  2. FILE (built-in)
  3. DESCRIPTION
  4. This module provides primitive operations to write multi-threaded programs.
  5. The ‘threading’ module provides a more convenient interface.
  6. CLASSES
  7. __builtin__.object
  8. lock
  9. exceptions.Exception(exceptions.BaseException)
  10. error
  11. FUNCTIONS
  12. ...
我们看到其并没有thread或相似的类,只包含了两个类lock&error以及一些方法。
现主要关注接口函数,暂时忽略其它:
①lock相关的函数说明:
分配一个锁:allocate_lock() -> lock object (allocate() is an obsolete synonym)
操作锁的方法:
acquire() — lock the lock, possibly blocking until it can be obtained
release() — unlock the lock
locked() — test whether the lock is currently locked
/* Deadlocks may ensue. */由程序员来保证不死锁。
②线程相关的函数说明:
创建一个新线程:start_new_thread(function,args[,kwargs]) (start_new() is an obsolete synonym) 
退出线程:exit()和exit_thread() (PyThread_exit_thread() is an obsolete synonym)

二,lock相关的函数使用示例(略去error):
  1. import thread

  2. def print_status(a_lock):
  3.     if a_lock.locked():
  4.         print “locked”
  5.     else:
  6.         print “not locked”
  7.         
  8. a_lock = thread.allocate_lock()
  9. print_status(a_lock)
  10. a_lock.acquire()
  11. print_status(a_lock)
  12. a_lock.release()
  13. print_status(a_lock)

三,thread相关的函数使用示例:</div>

  1. import thread

  2. def run(n):
  3.     # a back door, can not be run 4 times
  4.     if n == 4:
  5.         thread.exit()
  6.     for i in range(n):
  7.         print i

  8. thread.start_new_thread(run,(5,))
四,解决一个同步问题
试解决以下同步问题:使用两个线程交替输出"Hello"与"World"各5次,以"Hello"开始以"World"结束。
①HelloWorld问题的同步模型建立:

</p>
  1. semaphore h = 1, w = 0 
  2. # because the semaphore w is 0,so we should acquire the lock w to let it be zero(locked) when we use python to .
  3. thread1()
  4. {
  5.     while(true)
  6.     {
  7.         p(h)
  8.         do something;
  9.         v(w)
  10.     }
  11. }
  12. thread2()
  13. {
  14.     while(true)
  15.     {
  16.         p(w)
  17.         do something;
  18.         v(h)
  19.     }
  20. }
②使用Python实现上述同步模型,两个解决方案如下。
方案A用main线程和另一个线程来交替打印。
方案B使用除main线程外的另两个线程交替打印"Hello"与"World”。

</p>
  1. import thread

  2. def world():    
  3.     for i in range(5):
  4.         w_lock.acquire()    # i want to print world
  5.         print “world”
  6.         h_lock.release()    # you can print hello now
  7.     w_lock.release()

  8. # main thread
  9. print “use two threads to print hello&world”

  10. h_lock = thread.allocate_lock()
  11. w_lock = thread.allocate_lock()

  12. w_lock.acquire(); # “print world” can not be started first
  13. thread.start_new_thread(world,())
  14. for i in range(5):
  15.     h_lock.acquire()
  16.     print “hello”
  17.     w_lock.release()

  18. # raw_input(“finished”)


  1. import thread

  2. def hello():
  3.     for i in range(5):
  4.         h_ok.acquire()
  5.         print “hello”
  6.         w_ok.release()
  7. def world():
  8.     for i in range(5):
  9.         w_ok.acquire()        
  10.         print “world”
  11.         h_ok.release()

  12. # main thread
  13. print “use two threads to print hello&world”
  14. h_ok = thread.allocate_lock()
  15. w_ok = thread.allocate_lock()
  16. w_ok.acquire()
  17. thread.start_new_thread(hello,())
  18. thread.start_new_thread(world,())

  19. raw_input(“finished”) # !!it is necessary,in case main thread exit too early

原文地址:http://blog.chinaunix.net/uid-26438245-id-3164822.html

转载请注明:于哲的博客 » 【Python】thread解析