If a thread needs to immediately know when a consumer thread has processed a particular piece of data, it needs to bind the submitted data to an Event object that allows the manufacturer to track its progress, as shown in the code below —
Code # 1:
Writing multithreaded programs based on simple queuing is often a good way to maintain sanity. If everything is broken up into simple thread-safe queues, there is no need to clog the program with locks and other low-level synchronization. In addition, communication with queues often results in designs that can later be extended to other types of message-based communication patterns. For example, you can split your program into multiple processes, or even a distributed system, without changing much of your queue architecture.
One caveat with thread queuing is that queuing an item does not make a copy of the item. So communication actually involves passing an object reference between threads.
If general state is a concern, it might make sense to only pass immutable data structures (such as integers, strings, or tuples) or make deep copies of elements in queue as shown in the code below:
Code # 2:
# Stream that produces data
# Produce some data
out_q.put (copy.deepcopy (data))
# Stream that consumes data
# Get data
= < / code>
# Processing data
put ()blocks the producer.
Code # 3:
Methods get () and
put () support non-blocking and timeouts.
Both of these options can be used to avoid the problem of simply blocking a certain queue operation indefinitely. For example, the non-blocking
put () function can be used with a fixed-size queue to implement different types of processing code when the queue is full.
Code # 4: Issue a log message and discard
Timeout is useful if you are trying to force consumer threads to periodically abandon operations such as
q.get () so they can check things like the completion flag.
Code # 5: Using a Timeout
Finally, there are utility methods
q.qsize () ,
q.full () ,
q.empty () which can report the current size and state of the queue. Keep in mind, however, that they are all unreliable in a multithreaded environment. For example, a call to
q.empty () might say that the queue is empty, but in the time since the call was made, another thread might have added an item to the queue. Honestly, it's better to write code so as not to rely on such features.
In Learn Python 3 the Hard Way PDF, you'll learn Python by working through 52 brilliantly crafted exercises. Read them. Enter your code exactly. (No copying and pasting!) Correct your mistakes. ...
I remember one day, when I was about 15, my little cousin had come over. Being the good elder sister that I was, I spent time with her outside in the garden, while all the adults were inside having a ...
This is the first book on synthetic data for deep learning, and its extensive coverage could make this book the standard benchmark for synthetic data for years to come. The book can also serve as an i...
Python: - The Bible - 3 Manuscripts in 1 book: