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


Groups > comp.lang.python > #48126

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.053
X-Spam-Evidence '*H*': 0.89; '*S*': 0.00; 'messages.': 0.05; 'that?': 0.05; 'odd': 0.07; 'subject:two': 0.07; 'classes.': 0.09; 'fixed,': 0.09; 'sentence': 0.09; 'python': 0.11; 'def': 0.12; 'random': 0.14; 'thread': 0.14; 'advice:': 0.16; 'assignment?': 0.16; 'form),': 0.16; 'subject:threads': 0.16; 'thread,': 0.16; 'wendy': 0.16; 'wrote:': 0.18; 'import': 0.22; '(in': 0.22; 'python?': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'simpler': 0.24; 'post': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'program,': 0.31; 'code': 0.31; 'indentation': 0.31; 'pipe': 0.31; 'subject:numbers': 0.31; 'class': 0.32; 'probably': 0.32; 'run': 0.32; 'another': 0.32; 'text': 0.33; 'skip:_ 10': 0.34; 'message.': 0.35; 'advice': 0.35; 'there': 0.35; 'version': 0.36; 'html,': 0.36; 'next': 0.36; 'subject:?': 0.36; 'example,': 0.37; 'two': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'realize': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'read': 0.60; 'number,': 0.60; 'skip:t 30': 0.61; 'new': 0.61; 'numbers': 0.61; 'simply': 0.61; "you're": 0.61; 'first': 0.61; 'back': 0.62; 'offer': 0.62; 'name': 0.63; 'real': 0.63; 'telling': 0.64; 'more': 0.64; 'here': 0.66; 'frequently': 0.68; 'received:74.208': 0.68; 'goal': 0.75; 'lack': 0.78; 'fragment': 0.84; 'loses': 0.84; 'partially': 0.84; 'received:74.208.4.194': 0.84; 'start.': 0.84; 'hand,': 0.93
Date Fri, 14 Jun 2013 08:31:31 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6
MIME-Version 1.0
To python-list@python.org
Subject Re: how to use two threads to produce even and odd numbers?
References <1b3ddfd5-18ec-407b-b358-f795d154598b@googlegroups.com>
In-Reply-To <1b3ddfd5-18ec-407b-b358-f795d154598b@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:c6GeFh6azVkuOsxc6bUttM3inoMk4h7qkavxkY2L352 oUM3fTEOvNllh0dPG1+wMMfVD7c4UuJ6lPGqOiN2A1wrmJDe5/ qjmGPMZN0raXg/pOP7TjvF6ZFeI+AVq2V2Apba1fubTL/te9vk JJCWzPxnWW7X3XwK/o0SCWDTPT6Yh6J8gFGpesfPzlJEawqMPL 7KaaZL/00JLOyS5A8wRxgihH2wgi9vNBSHcW2ZceMJdJ+FiMcj EF3O3DDlwqbgP/sbe+yDFvaJ5hPUZuBUsFVhgEtu6DsJ1FhYNN RhlomW8Tg1bbmq+EhxEwwk/fLslYbI+kaeWnPGAbH+6xt0crdW pQhtFGMBcCtlMYkyQXZc=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3299.1371213105.3114.python-list@python.org> (permalink)
Lines 85
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1371213105 news.xs4all.nl 15913 [2001:888:2000:d::a6]:37673
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:48126

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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