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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to