Remaining tasks: 2Īdd_items - Processing queue size: 2. Remaining tasks: 1Īdd_items - Processing queue size: 1. In my computer, the execution results in this sequence of logs:Īdd_items - Processing queue size: 0. T_process = threading.Thread(target=process_items, args=(test_queue,), # insertion thread t_add = threading.Thread(target=add_items,Īrgs=(test_queue, 10), name= " add_items_thread") format(processing_queue.qsize(), processing_queue.unfinished_tasks)) def main(): Print( " add_items - Processing queue size: ". I will improve this point in a later step, to add some way to stop this thread in a controlled manner.ĭef add_items(processing_queue, num_items): The processing method includes an infinite loop ( while True), to make sure that the thread continues extracting elements when possible. We will simulate the time consumed processing the elements executing performing a sleep on the processing thread. I made the main thread join to the consumer thread, to wait for its completion.įor the moment, the actual processing code we will use in our program is not really relevant.I changed the number of inserted items to 10, instead of 100, to reduce the time to see the program end.I modified the logs so we can identify the thread from which we log.The associated method for the thread is process_items, and it accepts the queue from which it will extract items for processing as parameter. I added a new named thread for element extraction and processing to the code (a consumer thread).I made some changes to the code from my previous article on queues: I will follow my own recommendations on this in a later article, for the sake of simplicity. My recommendation is to use blocking for the get, and provide the call with an appropriate timeout (in seconds), to avoid issues with the uninterruptible wait. Use a threading lock object with the with statement to make it easier to acquire and release the lock.In this case, the exception raised is Queue.Empty, when no element can be extracted from the queue. Call the release() method of a lock object to release the previously acquired lock.Call the acquire() method of a lock object to acquire a lock.Use a threading lock object to prevent the race condition.A race condition occurs when two threads access a shared variable at the same time.T2 = Thread(target=increase, args=( 20,)) T1 = Thread(target=increase, args=( 10,)) The following example illustrates a race condition: from threading import ThreadĬounter = 0 def increase (by): global counter Whatever thread that changes the value last will win the race. The final value of the shared variable depends on which thread completes its update last. since the updates occur simultaneously, it creates a race to determine which thread’s modification is preserved. Then both threads attempt to change the value of the shared variable. At the same time, the second thread also reads the value from the same shared variable. In this scenario, the first thread reads the value from the shared variable. What is a race conditionĪ race condition occurs when two or more threads try to access a shared variable simultaneously, leading to unpredictable outcomes. Summary: in this tutorial, you’ll learn about race conditions and how to use the Python threading Lock object to prevent them.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |