NOTE : I know sue of global is not recommended. For many basic applications, the easy to use Pool interface. Output: C:\Users\dinesh\Desktop>python demo.py The concurrent.futures module is a well-kept secret in Python, but provides a uniquely simple way to implement threads and processes. T = threading.Thread(name=name,target=reader) #Read data from file and put it into list #Once data is not present, let's print exception message Then, the critical part, it notifies the queue that it's done with queue.taskdone, and resumes waiting for another item in the queue. When something is found, it grabs it and does its thing. Print threading.currentThread().getName() + " - " Yes, your subclass puts each thread in an infinite loop, waiting on something to be put in the queue. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. #Global variable list for reading file data multiprocessing is a package that supports spawning processes using an API similar to the threading module. from the queue a thread waiting to put is notified then. Once, all the data is printed and still thread trying to read, we will add the exception. import threading from collections import deque from heapq import heappush.Also, it will remove that printed line from list. The Producer thread is responsible for putting items into the queue if it is not full while the Consumer thread consumes items if there are any. Now, every thread will read one line from list and print it.So, instead of opening file in every thread and read it, we will open file one time and put data in list. You want to read each line of file in new thread.In your case, every thread is reading all messages because target function " job" is opening file and reading all the data and every thread is calling that target function. Let me answer regarding multi-threading only. Your trying too many things to learn in same code snippet like 1) Multi-Threading 2) Queue Data Structure 3) Thread Synchronization Mechanisms 4) Locking etc.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |