Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #48117 > unrolled thread

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

Started byZoe Wendy <xhtldbjbdv@gmail.com>
First post2013-06-14 04:50 -0700
Last post2013-06-15 02:18 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#48117 — how to use two threads to produce even and odd numbers?

FromZoe Wendy <xhtldbjbdv@gmail.com>
Date2013-06-14 04:50 -0700
Subjecthow to use two threads to produce even and odd numbers?
Message-ID<1b3ddfd5-18ec-407b-b358-f795d154598b@googlegroups.com>
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'

[toc] | [next] | [standalone]


#48126

FromDave Angel <davea@davea.name>
Date2013-06-14 08:31 -0400
Message-ID<mailman.3299.1371213105.3114.python-list@python.org>
In reply to#48117
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

[toc] | [prev] | [next] | [standalone]


#48243

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-06-15 02:18 +0000
Message-ID<51bbcefc$0$29997$c3e8da3$5496439d@news.astraweb.com>
In reply to#48117
On Fri, 14 Jun 2013 04:50:05 -0700, Zoe Wendy wrote:

> 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?


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web