Created
August 28, 2017 07:38
-
-
Save feliperyan/17a631006698a175a609f50b7b04feb6 to your computer and use it in GitHub Desktop.
Example of Threading in Python from https://www.tutorialspoint.com/python/python_multithreading.htm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import Queue | |
import threading | |
import time | |
exitFlag = 0 | |
class myThread (threading.Thread): | |
def __init__(self, threadID, name, q): | |
threading.Thread.__init__(self) | |
self.threadID = threadID | |
self.name = name | |
self.q = q | |
def run(self): | |
print "Starting " + self.name | |
process_data(self.name, self.q) | |
print "Exiting " + self.name | |
def process_data(threadName, q): | |
# fr this method basically keeps checking the workqueue and processes anything in there | |
# if there's nothing, it sleeps for 1 sec. | |
while not exitFlag: | |
queueLock.acquire() # fr Notice it's the same object initiated below - weird var scope much? | |
if not workQueue.empty(): # fr again a var declared outside of this method | |
data = q.get() # fr Can the queue handle the locking bit? It's supposed to be able to | |
queueLock.release() # fr So now other Threads can do their things | |
print("%s processing %s" % (threadName, data)) | |
else: | |
print("%s sleeping, nothing in queue" % (threadName, data)) | |
queueLock.release() | |
time.sleep(1) | |
threadList = ["Thread-1", "Thread-2", "Thread-3"] | |
nameList = ["One", "Two", "Three", "Four", "Five"] | |
# fr Gonna pass this Lock object around the place | |
queueLock = threading.Lock() | |
workQueue = Queue.Queue(10) | |
threads = [] | |
threadID = 1 | |
# Create new threads | |
for tName in threadList: # fr also notice that threads are starting but sitting there doing nothing as the queue is empty | |
thread = myThread(threadID, tName, workQueue) | |
thread.start() | |
threads.append(thread) | |
threadID += 1 | |
# Fill the queue | |
queueLock.acquire() # fr now we acquire a lock to fill the queue, but did we need it? OR... | |
# are we locking so we can fill it up completely and THEN process it... aaaah! | |
for word in nameList: | |
workQueue.put(word) | |
queueLock.release() | |
# Wait for queue to empty | |
while not workQueue.empty(): # fr as we're still processing the main thread this of course is run | |
# Cant imagine it will take long though, but could move the timer around so it takes longer. | |
pass | |
# Notify threads it's time to exit | |
# fr now set flag, thread can be at any stage of execution so will eventually hit that | |
# section with the 'while not exitFlag' | |
exitFlag = 1 | |
# Wait for all threads to complete | |
# fr surely some of the threads have already completed? | |
for t in threads: | |
t.join() | |
print "Exiting Main Thread" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you please briefly explain what does exitFlag() variable does in this code?