Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Python Testing Marcin Młotkowski 10th April, 2013 Threads Shared resources 1 Threads 2 Shared resources Marcin Młotkowski Python Threads Shared resources Introduction From Wikipedia A thread of execution is the smallest unit of processing that can be scheduled by an operating system. Threads of the same process share common code and data, but have independent stacks. Marcin Młotkowski Python Threads Shared resources Why use threads to deal with slow I/O operations simultaneously (for example: downloading files); simultaneous requests processing, e.g. Web servers. Marcin Młotkowski Python Threads Shared resources An example of two-threaded computation 1st example 1st thread i=i+1 print i i=i+1 print i i=i+1 print i 2nd example 2nd thread 1st thread i=i+1 2nd thread i=i+1 print i print i i=i+1 i=i+1 i=i+1 print i i=i+1 print i i=i+1 print i Marcin Młotkowski print i print i i=i+1 i=i+1 print i print i Python Threads Shared resources Threads in Python thread (3.0: _thread): low-level library threading: high-level library Marcin Młotkowski Python Threads Shared resources Threads in Python thread (3.0: _thread): low-level library threading: high-level library dummy_thread dummy_threading multiprocessing Marcin Młotkowski Python Threads Shared resources How to use threads threading module class Thread: def run(self): """Code executed in thread""" def start(self): """Start a thread""" Marcin Młotkowski Python Threads Shared resources Example task Simulate a race, e.g. a marathon. Marcin Młotkowski Python Threads Shared resources Runners implementation import threading total_distance = 0 class runner(threading.Thread): def __init__(self, nr_startowy): self.numer = nr_startowy threading.Thread.__init__(self) Marcin Młotkowski Python Threads Shared resources Race implementation class runner, cd def run(self): global total_distance dystans = 42195 while dystans > 0: distance = distance - 1 total_distance = total_distance + 1 if distance % 10000 == 0: print ’Runner no %i’ % self.numer print ’Runner %i at finishing-line’ % self.numer Marcin Młotkowski Python Threads Shared resources Run a race r1 = runner(1) r2 = runner(2) r1.start() r2.start() r1.join() r2.join() print ’End of race, distance’, total_distance Marcin Młotkowski Python Threads Shared resources What is .join The main program is also a thread, so after r1.start() there are two threads r1.join() means, that "primary" thread will wait until r1 finishes. Marcin Młotkowski Python Threads Shared resources examples of run Runner no 1 Runner no 2 Runner no 2 Runner no 2 Runner no 1 Runner no 1 Runner no 2Runner no 1 Runner Runner Runner Runner no 1 1 at finishing line no 2 2 at finishing line Marcin Młotkowski Runner no 1Runner no 2 Runner no 1 Runner no 1 Runner no 2 Runner no 2 Runner no 1 Runner no 1 Runner no 2 Runner no 2 Runner 2 at finishing line Runner 1 at finishing line Python Threads Shared resources Common variables Reminder total_distance = 0 class runner(threading.Thread): ... total_distance = total_distance + 1 print total_distance Marcin Młotkowski Python Threads Shared resources A puzzle What is the value of the variable total_distance? Marcin Młotkowski Python Threads Shared resources A puzzle What is the value of the variable total_distance? Theory 2 * 42195 = 84390 Marcin Młotkowski Python Threads Shared resources A puzzle What is the value of the variable total_distance? Theory 2 * 42195 = 84390 Experience 54390 74390 83464 ... Marcin Młotkowski Python Threads Shared resources Atomic operations? i=i+1 LOADFAST 0 LOAD_CONST 1 BINARY_ADD STORE_FAST 0 Marcin Młotkowski Python Threads Shared resources Atomic operations? i=i+1 LOADFAST 0 LOAD_CONST 1 BINARY_ADD STORE_FAST 0 i=i+1 LOADFAST 0 LOAD_CONST 1 BINARY_ADD STORE_FAST 0 Marcin Młotkowski Python Threads Shared resources Locks A class Lock lock = Lock() def run(self): global lock ... lock.acquire() total_distance = total_distance + 1 lock.release() Marcin Młotkowski Python Threads Shared resources Other locks RLock A thread may lock any number of times, and as many times release it. It slows down computation dramatically. Semaphore Lock a fixed number of times: sem = Semaphore(3) sem.acquire() sem.acquire() sem.acquire() sem.acquire() # lock Marcin Młotkowski Python Threads Shared resources Waiting for resource One thread — a bartender – is pouring a glass of milk, the second — a customer — is waiting until the glass is full and drink. Marcin Młotkowski Python Threads Shared resources Implementation lck = Lock() Pouring lck.acquire() for i in range(5): glass_of_milk = glass_of_milk + 1 lck.release() Drinking while glass_of_milk != 5: pass lck.acquire() while glass_of_milk > 0: glass_of_milk = glass_of_milk - 1 lck.release() Marcin Młotkowski Python Threads Shared resources Implementation lck = Lock() Pouring lck.acquire() for i in range(5): glass_of_milk = glass_of_milk + 1 lck.release() Drinking while glass_of_milk != 5: pass lck.acquire() while glass_of_milk > 0: glass_of_milk = glass_of_milk - 1 lck.release() Marcin Młotkowski Python Threads Shared resources Conditional variables Conditional variables allow to put to sleep and wake threads. Marcin Młotkowski Python Threads Shared resources Implementation lck = threading.Condition() Consume lck.acquire() while glass_of_milk != 5: lck.wait() while glass_of_milk > 0: glass_of_milk = glass_of_milk - 1 lck.release() Pouring lck.acquire() for i in range(5): glass_of_milk = glass_of_milk + 1 lck.notify() lck.release() Marcin Młotkowski Python Threads Shared resources Conditional variables Conditional variables act as locks (acquire(), release()); a method wait() releases a lock and put to sleep a current thread; a method notify() wakes one of sleeping thread (related to this cond. var.), notifyAll() wakes all sleeping threads. Marcin Młotkowski Python Threads Shared resources Disadvantages there is only one glass of milk, which can be either poured or someone drinks the bartender cannot pour more glasses of milk and go home Marcin Młotkowski Python Threads Shared resources Data structures for multi-threaded programs A class Queue: It is a queue FIFO, thread–safe; Constructor: Queue(size ) get an element and remove: .get(); if the queue is empty raises an exception Empty .get(True): if the queue is empty, it is put to sleep; insert an element: .put(element ), if the queue is full an exception Full is raised; insert an element: .put(element, True), if the queue is full the thread is put to sleep; .full(), .empty() Marcin Młotkowski Python Threads Shared resources Variants of Queue LifoQueue PriorityQueue Marcin Młotkowski Python