4
\$\begingroup\$

I'm just beginning to get the idea of queue and threading.

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 yea, well I put 19073486328125 in the queue!
       19073486328125!?!?
Oh yea, well I put 95367431640625 in the queue!
       95367431640625!?!?
Oh yea, well I put 476837158203125 in the queue!
476837158203125 is a BIG number, man. Wow. Look. We made a pyramid.
\$\endgroup\$
1
  • \$\begingroup\$ i can already see that the function should be documented. 'retrieve number from queue and replace it with number*5' or something along those lines. \$\endgroup\$
    – MikeiLL
    Commented Oct 2, 2014 at 16:52

1 Answer 1

2
\$\begingroup\$

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:

from threading import Thread as T
from Queue import Queue as Q
from random import randint as R

Just stick to good old-fashioned:

from threading import Thread
from Queue import Queue
from random import randint

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.

\$\endgroup\$
3
  • \$\begingroup\$ The original title actually contained the word newbie, but was edited by admins. Sorry if I got your hopes up that it might be interesting. \$\endgroup\$
    – MikeiLL
    Commented Oct 2, 2014 at 17:43
  • 2
    \$\begingroup\$ I hope I didn't offend you. I meant "interesting" not as "this is no fun for me get lost" but as "there's not a lot to review in this piece, but look, there's a cool doc about this, check it out you'll like it" ;-) \$\endgroup\$
    – janos
    Commented Oct 2, 2014 at 17:57
  • \$\begingroup\$ I think my ego did some into play a little bit. I have read through pymotw.com/2/Queue a few times, which offers a slightly more fleshed out version of the example in the Python docs. Learning it has felt like a slow process (pun intended). \$\endgroup\$
    – MikeiLL
    Commented Oct 2, 2014 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.