how to use two threads to produce even and odd numbers?

Z

Zoe Wendy

I am going to compile a small python program in order to use Queue to produce a random with a thread. For example, using one thread to print odd number, while another thread to print even number.

Here is my codes, please offer me some advice:


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'
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'
 
D

Dave Angel

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.
 
S

Steven D'Aprano

I am going to compile a small python program in order to use Queue to
produce a random with a thread. For example, using one thread to print
odd number, while another thread to print even number.

Here is my codes, please offer me some advice:

Unfortunately your code has been mangled by your mail program. Please
turn off so-called "Rich Text", or HTML mail, since that causes the
formatting of the code to be mangled. I will try to reconstruct the
formatting, but if I get it wrong, don't blame me :)

I have also fixed a couple of syntax errors, which are shown with
comments below.


# === your code -- reconstructed ===
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): *** THIS LINE IS WRONG ***
for i in range(1, 200, 2):
print self.getName(), 'adding', i, 'to queue'
self.sharedata.put(i)
time.sleep(random.randrange(10)/10.0)
print self.getName(),'Finished'

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): *** THIS LINE IS WRONG ***
for i in range(0, 200, 2):
print self.getName(), 'got a value:', self.sharedata.get()
time.sleep(random.randrange(10)/10.0)
print self.getName(),'Finished'

# === end code ===


You have one thread, jishu, that feeds *odd* numbers into a queue, and
another thread, oushu, that takes them out. Neither thread actually gets
run though: you create the classes, but you don't start the threads
running.

Add this to the end of the code:


data = Queue()
a = jishu('jishu', data)
b = oushu('oushu', data)
a.start()
time.sleep(2)
b.start()


and then run the program, and you will see output like this, only much
more of it:




jishu adding 1 to queue
jishu adding 3 to queue
jishu adding 5 to queue
jishu adding 7 to queue
[...]
oushu got a value: 1
jishu adding 61 to queue
jishu adding 63 to queue
oushu got a value: 3
jishu adding 65 to queue
oushu got a value: 5
oushu got a value: 7
[...]
oushu got a value: 183
jishu adding 199 to queue
jishu Finished
oushu got a value: 185
oushu got a value: 187
oushu got a value: 189
oushu got a value: 191
oushu got a value: 193
oushu got a value: 195
oushu got a value: 197
oushu got a value: 199
oushu Finished



Or something similar to that.


Does this help?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top