Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #48126
| Date | 2013-06-14 08:31 -0400 |
|---|---|
| From | Dave Angel <davea@davea.name> |
| Subject | Re: how to use two threads to produce even and odd numbers? |
| References | <1b3ddfd5-18ec-407b-b358-f795d154598b@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3299.1371213105.3114.python-list@python.org> (permalink) |
On 06/14/2013 07:50 AM, Zoe Wendy wrote: Welcome to the forum. Are you new to Python? Are you new to programming? What version of Python are you using? Is this a class assignment? > I am going to compile a small python program in order to use Queue to produce a random with a thread. That sentence doesn't parse. > For example, using one thread to print odd number, while another thread to print even number. > Is there a reason for that? What's your real goal? > Here is my codes, please offer me some advice: First advice is not to post in html, as it frequently loses indentation (and other things) All of your code fragment is at the left margin. Please post as text message. And avoid googlegroups, as it will greatly reduce the number of people willing to read your messages. If you're subscribed to the list (in non-digest form), simply use your email program, telling it to post as text messages. Next - you don't actually use the Queue. > > > import threading > import random > import time > from Queue import Queue > > class jishu (threading.Thread): > > def __init__(self, threadname, queue): > threading.Thread.__init__(self, name = threadname) > self.sharedata = queue > > def run(self): > for i %2 == 1 in range(200): > print self.getName(),'adding',i,'to queue' This will intermix parts of the line with the ones printed by the other thread. > self.sharedata.put(i) > time.sleep(random.randrange(10)/10.0) > print self.getName(),'Finished' > > > # oushu thread > > class oushu(threading.Thread): > > > def __init__(self, threadname, queue): > threading.Thread.__init__(self, name = threadname) > self.sharedata = queue > > > def run(self): > > for i %2 == 0 in range(200): > print self.getName(),'got a value:',self.sharedata.get() > time.sleep(random.randrange(10)/10.0) > print self.getName(),'Finished' > Once the indentation is fixed, this code won't actually do anything because you lack any code to instantiate these classes. Did you mean to use the queues to pipe the text back to the main thread, so that it can be printed discretely? You do realize that compute-bound threads are counterproductive? Two threads will probably take much longer to run than one. If your real goal is to partially randomize the order that the numbers are picked, there are much simpler ways to do it. On the other hand, if this is an assignment, or if it's leading up to a much more complex problem, then it's a good start. -- DaveA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to use two threads to produce even and odd numbers? Zoe Wendy <xhtldbjbdv@gmail.com> - 2013-06-14 04:50 -0700 Re: how to use two threads to produce even and odd numbers? Dave Angel <davea@davea.name> - 2013-06-14 08:31 -0400 Re: how to use two threads to produce even and odd numbers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-15 02:18 +0000
csiph-web