Pythonthread Lock LockObject py lock object import thread
Pythonのthreadモジュール Lockオブジェクトの使用例 Lock_Object. py # lock object import thread, time n=0 lck = thread. allocate_lock() def fadd() : global n, lck while 1: if lck. acquire() == 0 : pass else : n += 1 lck. release() time. sleep(2) def fdec() : global n, lck while 1: if lck. acquire() == 0 : pass else : n -= 1 lck. release() time. sleep(3) thread. start_new_thread(fadd, ()) thread. start_new_thread(fdec, ()) while 1: print n time. sleep(2) Lock_Object. py
Pythonのthreadモジュール スレッドの使用例: spawn_thread. py l l threadを次々と生成する. . mutexなし. . import sys, time import thread def child. Thread(thread_id): while 1: print ' Hello from thread', thread_id time. sleep(1) def parent. Thread(): thread_id = 0 while 1: thread_id += 1 print ' Creating new thread' thread. start_new(child. Thread, , )) thread. start_new(child. Thread, (thread_id, )) print ' Destroy new thread' if raw_input() == 'q': break print "press 'q' to quit. " if __name__ == "__main__": print "Test with", sys. argv[0] parent. Thread() spawn_thread. py
Pythonのthreadモジュール スレッドの使用例: spawn_thread_exit. py l threadクラスを使うメリット: : 明示的にスレッド処理を停止する exit() メソッドが提供されている l threadを次々と生成する. . mutexなし. . import sys, time import thread def child. Thread(thread_id): n=0 while 1: print ' Hello from thread', thread_id, 'n', n time. sleep(3) n += 1 if n > 5: thread. exit() def parent. Thread(): thread_id = 0 while 1: thread_id += 1 print ' Creating new thread' thread. start_new(child. Thread, , )) thread. start_new(child. Thread, (thread_id, )) print ' Destroy new thread' if raw_input() == 'q': break print "press 'q' to quit. " if __name__ == "__main__": print "Test with", sys. argv[0] parent. Thread() spawn_thread_exit. py
Pythonのthreadモジュール スレッドの使用例: thread_count. py l l l threadを生成して, 自分のidとループindexを表示する. mutexなしなので, あるスレッドのループが完全に終了する前に, 他スレッドのループ出力が表示される可能性がある. import sys, time import thread def child. Thread(my_id, count): for i in range(0, count): time. sleep(1. 0) print ' [my_id = %s] => %s' % (my_id, i) def parent. Thread(): for my_id in range(0, 10): print 'Create new thread [my_id = %s]' % (my_id, ) thread. start_new(child. Thread, (my_id, 3)) print 'Destroy new thread [my_id = %s]' % (my_id, ) time. sleep(1. 0) if __name__ == "__main__": print "Test with", sys. argv[0] parent. Thread()
Pythonのthreadモジュール スレッドの使用例: thread_mutex. py l l l threadを生成して とループindexを表示する. . threadを生成して, , 自分のidとループ mutexあり. . ループ開始時にmutex を取得しているので, , ループ開始時にmutexを取得しているので あるスレッドのループ処理中に, 他スレッドがループ表示をすることがない. import sys import thread, time def child. Thread(my_id, count): mutex. acquire() for i in range(count): time. sleep(1) print ' [my_id = %s] => %s' % (my_id, , i) mutex. release() def parent. Thread(): global mutex = thread. allocate_lock() for my_id in range(10): print 'Creating new thread[my_id = %s]' % (my_id , ) (my_id, ) thread. start_new_thread(child. Thread, (my_id, , 3)) print 'Destroy new thread[%s]' , ) thread[%s]' % (my_id, ) time. sleep(1) if __name__ == "__main__": print "Test with", sys. argv[0] parent. Thread()
Pythonのthreadモジュール スレッドの使用例: thread_count_wait. py l l counter()関数のスレッドを作る. . mutexあり. . stdout用の mutex. . stdout用のmutex すべてのスレッドが死ぬまで, 手動でブロックする. import sys, time import thread stdout_mutex = thread. allocate_lock() exit_mutexes = [0] * 10 def child. Thread(my_id, count): global exit_mutexes for i in range(count): stdout_mutex. acquire() print ' [my_id = %s] => %s' % (my_id, , i) stdout_mutex. release() exit_mutexes[my_id] = 1 def parent. Thread(): for i in range(10): print 'Create new thread [my_id , ) [my_id = %s]' % (my_id, ) thread. start_new(counter, (i, 100)) print 'Destroy new thread [my_id , ) [my_id = %s]' % (my_id, ) while 0 in exit_mutexes: pass if __name__ == "__main__": print "Test with", sys. argv[0] parent. Thread()
- Slides: 11