patternpythonMinor
Threading and queue experiment
Viewed 0 times
andthreadingqueueexperiment
Problem
I'm just beginning to get the idea of queue and threading.
I would welcome input on this exercise script:
Which outputs something along the lines of:
` I put 5 in the queue.
5!?!?
Oh yea, well I put 25 in the queue!
25!?!?
Oh yea, well I put 125 in the queue!
125!?!?
Oh yea, well I put 625 in the queue!
625!?!?
Oh yea, well I put 3125 in the queue!
3125!?!?
Oh yea, well I put 15625 in the queue!
15625!?!?
Oh yea, well I put 78125 in the queue!
78125!?!?
Oh yea, well I put 390625 in the queue!
390625!?!?
Oh yea, well I put 1953125 in the queue!
1953125!?!?
Oh yea, well I put 9765625 in the queue!
9765625!?!?
Oh yea, well I put 48828125 in the queue!
48828125!?!?
Oh yea, well I put 244140625 in the queue!
244140625!?!?
Oh yea, well I put 1220703125 in the queue!
1220703125!?!?
Oh yea, well I put 6103515625 in the queue!
6103515625!?!?
Oh yea, well I put 30517578125 in the queue!
30517578125!?!?
Oh yea, well I put 152587890625 in the queue!
152587890625!?!?
Oh yea, well I put 762939453125 in the queue!
762939453125!?!?
Oh yea, well I put 3814697265625 in the queue!
3814697265625!?!?
Oh ye
I would welcome input on this exercise script:
from threading import Thread as T
from Queue import Queue as Q
from random import randint as R
import time
'''
Silly script exploring simple use of threadding.Thread and Queue.Queue
'''
q = Q()
now = time.time()
def one_up(queue):
while time.time() - now <= 20:
other_num = queue.get()
big_num = 5*other_num
print(" {}!?!?".format(other_num))
queue.put(big_num)
print("Oh yea, well I put {} in the queue!".format(big_num))
time.sleep(1)
print("{} is a BIG number, man. Wow. Look. We made a pyramid.".format(big_num))
a_num = R(1,10)
q.put(a_num)
print("I put {} in the queue.".format(a_num))
t = T(target=one_up, args=(q,))
t.start()Which outputs something along the lines of:
` I put 5 in the queue.
5!?!?
Oh yea, well I put 25 in the queue!
25!?!?
Oh yea, well I put 125 in the queue!
125!?!?
Oh yea, well I put 625 in the queue!
625!?!?
Oh yea, well I put 3125 in the queue!
3125!?!?
Oh yea, well I put 15625 in the queue!
15625!?!?
Oh yea, well I put 78125 in the queue!
78125!?!?
Oh yea, well I put 390625 in the queue!
390625!?!?
Oh yea, well I put 1953125 in the queue!
1953125!?!?
Oh yea, well I put 9765625 in the queue!
9765625!?!?
Oh yea, well I put 48828125 in the queue!
48828125!?!?
Oh yea, well I put 244140625 in the queue!
244140625!?!?
Oh yea, well I put 1220703125 in the queue!
1220703125!?!?
Oh yea, well I put 6103515625 in the queue!
6103515625!?!?
Oh yea, well I put 30517578125 in the queue!
30517578125!?!?
Oh yea, well I put 152587890625 in the queue!
152587890625!?!?
Oh yea, well I put 762939453125 in the queue!
762939453125!?!?
Oh yea, well I put 3814697265625 in the queue!
3814697265625!?!?
Oh ye
Solution
Single-letter variable names are usually a bad practice, with few exceptions (loop counters, caught exception objects). Importing modules with single-letter names is very silly indeed. So don't do this:
Just stick to good old-fashioned:
This is not a very interesting experiment with threading and a Queue. Check out the documentation, it's quite basic but much more interesting, and really using threading and a Queue.
from threading import Thread as T
from Queue import Queue as Q
from random import randint as RJust stick to good old-fashioned:
from threading import Thread
from Queue import Queue
from random import randintThis is not a very interesting experiment with threading and a Queue. Check out the documentation, it's quite basic but much more interesting, and really using threading and a Queue.
Code Snippets
from threading import Thread as T
from Queue import Queue as Q
from random import randint as Rfrom threading import Thread
from Queue import Queue
from random import randintContext
StackExchange Code Review Q#64557, answer score: 2
Revisions (0)
No revisions yet.