mirror of
https://github.com/dbarzin/pandora-box.git
synced 2025-07-22 23:09:41 +02:00
work on example
This commit is contained in:
parent
64b0ad73a9
commit
f1b9342f98
1 changed files with 16 additions and 20 deletions
|
@ -4,53 +4,48 @@ import queue
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
exitFlag = False
|
||||||
exitFlag = 0
|
|
||||||
|
|
||||||
|
|
||||||
class myThread (threading.Thread):
|
class myThread (threading.Thread):
|
||||||
def __init__(self, threadID, name, q):
|
def __init__(self, id, q):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
self.threadID = threadID
|
self.id = id
|
||||||
self.name = name
|
|
||||||
self.q = q
|
self.q = q
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
print("Starting " + self.name)
|
print(f"Thread-{self.id} Starting ")
|
||||||
process_data(self.name, self.q)
|
process_data(self.id, self.q)
|
||||||
print("Exiting " + self.name)
|
print(f"Thread-{self.id} Done.")
|
||||||
|
|
||||||
|
|
||||||
def process_data(threadName, q):
|
def process_data(id, q):
|
||||||
while not exitFlag:
|
while not exitFlag:
|
||||||
queueLock.acquire()
|
queueLock.acquire()
|
||||||
if not workQueue.empty():
|
if not workQueue.empty():
|
||||||
data = q.get()
|
data = q.get()
|
||||||
queueLock.release()
|
queueLock.release()
|
||||||
print("%s processing %s" % (threadName, data))
|
print(f"Thread-{id} processing {data}")
|
||||||
else:
|
else:
|
||||||
queueLock.release()
|
queueLock.release()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
threadList = ["Thread-1", "Thread-2"]
|
maxThread = 2
|
||||||
nameList = ["One", "Two", "Three", "Four", "Five", "Six", 'Seven']
|
workList = ["One", "Two", "Three", "Four", "Five", "Six", 'Seven']
|
||||||
queueLock = threading.Lock()
|
queueLock = threading.Lock()
|
||||||
workQueue = queue.Queue(10)
|
workQueue = queue.Queue()
|
||||||
threads = []
|
threads = []
|
||||||
threadID = 1
|
|
||||||
|
|
||||||
|
|
||||||
# Create new threads
|
# Create new threads
|
||||||
for tName in threadList:
|
for i in range(maxThread):
|
||||||
thread = myThread(threadID, tName, workQueue)
|
thread = myThread(i, workQueue)
|
||||||
thread.start()
|
thread.start()
|
||||||
threads.append(thread)
|
threads.append(thread)
|
||||||
threadID += 1
|
|
||||||
|
|
||||||
# Fill the queue
|
# Fill the queue
|
||||||
queueLock.acquire()
|
queueLock.acquire()
|
||||||
for word in nameList:
|
for word in workList:
|
||||||
workQueue.put(word)
|
workQueue.put(word)
|
||||||
queueLock.release()
|
queueLock.release()
|
||||||
|
|
||||||
|
@ -59,9 +54,10 @@ while not workQueue.empty():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Notify threads it's time to exit
|
# Notify threads it's time to exit
|
||||||
exitFlag = 1
|
exitFlag = True
|
||||||
|
|
||||||
# Wait for all threads to complete
|
# Wait for all threads to complete
|
||||||
for t in threads:
|
for t in threads:
|
||||||
t.join()
|
t.join()
|
||||||
|
|
||||||
print("Exiting Main Thread")
|
print("Exiting Main Thread")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue