[Tutor] Fwd: Concept of multithreading
Dennis Lee Bieber
wlfraed at ix.netcom.com
Tue Aug 24 10:51:37 EDT 2021
On Mon, 23 Aug 2021 16:59:31 -0400, Dennis Lee Bieber
<wlfraed at ix.netcom.com> declaimed the following:
> Note that the time output is wall-clock durations, and mostly counts
>the time spent sleeping (or for main thread, blocked trying to queue the
>next value).
I tried changing the time.perf_counter() into time.thread_time_ns() and
time.process_time_ns(), neither of which include the time spent in sleep()
(nor, I suspect, any other blocking operation), but so little work is
actually done each time the thread(s) wake up that the accumulated times
came out as 0ns. This even after I doubled the work, cut the sleep() times
to average 0.5s, and moved the generation of random integers and random
sleep times /into/ the worker threads. The main thread is just queuing
integers to wake up the workers -- the integer is otherwise ignored.
-=-=-=-
C:\Users\Wulfraed\Documents\_Hg-Repositories\Python Progs>Q_Threads.py
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .
Worker: 2 Sum: 22722 Number Values: 181 Time: 83.207052
Worker: 0 Sum: 17799 Number Values: 151 Time:
83.56292210000001
Worker: 1 Sum: 21747 Number Values: 168 Time: 83.8785809
Total time: 83.8851056
C:\Users\Wulfraed\Documents\_Hg-Repositories\Python Progs>
-=-=-=-
DIFF format rather than full listing of source...
@@ -11,5 +11,6 @@
DONE = object()
NUMWORKERS = 3
-NUMVALUES = 250 #with average sleep of 1.0 second, will take 4 minutes
to complete
+NUMVALUES = 500 #with average sleep of 0.5 second, will take 4 minutes
to complete
+ #IF done linearly. with three threads, about 85
seconds each
@@ -15,4 +16,7 @@
+#tried using time.thread_time and time.process_time as they do not count
+#time spent in sleep() or otherwise blocked, but the degree of work done
+#on each cycle is so small the accumulated times were 0.0!
def worker(id, inQ, outQ):
sumv = 0
numv = 0
@@ -16,7 +20,7 @@
def worker(id, inQ, outQ):
sumv = 0
numv = 0
- startPC = time.perf_counter()
+ start = time.perf_counter()
while True:
vlu = inQ.get()
if vlu == DONE: break
@@ -20,5 +24,7 @@
while True:
vlu = inQ.get()
if vlu == DONE: break
- sumv += vlu[0]
+ #increment count of processed items
+ #generate random value ("work")
+ #generate random sleep between 0 and 1 second
numv += 1
@@ -24,4 +30,6 @@
numv += 1
- time.sleep(vlu[1])
- inQ.put(vlu) #propagate the DONE flag to next
+ sumv += random.randint(1, NUMVALUES // 2)
+ time.sleep(random.random())
+ inQ.put(vlu) #propagate the DONE flag to next thread
+ #return results
outQ.put((id, sumv, numv,
@@ -27,3 +35,3 @@
outQ.put((id, sumv, numv,
- time.perf_counter() - startPC))
+ time.perf_counter() - start))
@@ -29,8 +37,8 @@
-startPC = time.perf_counter()
+start = time.perf_counter()
threads = [threading.Thread(target=worker, args=(i, inQueue, outQueue))
for i in range(NUMWORKERS)]
for t in threads:
t.start()
@@ -31,13 +39,12 @@
threads = [threading.Thread(target=worker, args=(i, inQueue, outQueue))
for i in range(NUMWORKERS)]
for t in threads:
t.start()
-for _ in range(NUMVALUES):
- v = random.randint(1, NUMVALUES // 2)
- s = random.random() * 2.0
- inQueue.put((v, s)) #will block if 10 items are in the queue,
letting thread(s) run
+for i in range(NUMVALUES):
+ #i is put in queue only to release next worker, it is otherwise
meaningless
+ inQueue.put(i) #will block if 10 items are in the queue, letting
thread(s) run
print(". ", end="", flush=True) #progress indicator
print("\n\n")
@@ -52,5 +59,5 @@
print("Worker: %s\tSum: %s\tNumber Values: %s\tTime: %s"
% (id, sm, nm, dur))
-print("Total timePC: %s" %
- (time.perf_counter() - startPC))
+print("Total time: %s" %
+ (time.perf_counter() - start))
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Tutor
mailing list