![]() sleep ( 3 ) print ( "consumer put value" ) priority = random. pop ()) def consumer (): while True : time. _q ) return ret priq = PriorityQueue () def producer (): while True : print ( priq. _q def put ( self, item, priority ): with self. Condition () def _str_ ( self ): return str ( self. If you want a copy of the code, let me know and I’ll update this post with a zipped copy of it.Import threading import heapq import time import random class PriorityQueue ( object ): def _init_ ( self ): self. ![]() I just finished adding spaces to show where a def ends, and the damn code highlighter removed it again. # Call the mainfunction that sets up threading. # Executes if the program is started normally, not if imported # Let the queue know the job is finished. Print "Spawning the ".format(job,job,(job*job)) # This could use some more try:except code. # And then waits for the threads to finish # It spawns the threads, fills up the queue with work items that the threads will use Singlelock = threading.Lock() # This is a lock so threads don't print trough each other (and other reasons) Jobs = Queue.Queue(5) # This sets up the queue object to use 5 slots THREAD_LIMIT = 3 # This is how many threads we want Still a bit stumped on getting more info, name, etc on the thread that is running at the moment – haven’t figured that out yet how to do that.įeel free to comment and ask questions – if you can improve this program, please let me know !. ![]() Not sure if I understand completely all the possibilities this offers. When printing out things to the console or python shell, things got jumbled because different threads took over from each other – to solve that I used the threading.Lock().acquire() and threading.Lock().release() to make sure that a thread could finish printing.Duh! You spawn the threads before you fill up the queues with stuff todo….This example is deliberately made as simple as possible so to understand the basic principles of threading and the queue system. However, while their examples undoubtedly do more and are more extensive, they are also more complex. I based most of my simple example on the examples in the Python threading tutorial (.pdf) work of Norman Matloff and Francis Hsu that I referenced before in a previous blog post. join() function.Īll spawned threads keep on being active, running forever, accepting jobs – that is, until the queue is empty, at which point they shut down. You will notice in the console print that the message “Waiting for threads to finish.” appears after the fifth result, indicating that the queues are being used and the main program has continued on.Īfter putting everything in the queue system, the program waits for the threads to finish using the. The Queue system itself is limited to 5 slots, although this could easily be changed to more or less. Those numbers are divided over those 3 threads by the Queue system. The example below uses 3 threads, and processes 10 pairs of numbers (tuples) that I put in a list. And re-use this later on, in case I need to revisit this again it would be handy not to scour the internet again to assemble the bits and pieces of threading with Python. I managed to write a really simple example of using threads in Python that I hope will give more insight on how to adapt my other programming stuff.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |