0

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' 

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
18
Views
4 Years
Discussion Span
Last Post by Lucaci Andrew
1

Maybe something along this snippets will help you: Click Here

Also, this

for i %2 == 1 in range(200):

never, use the step argument from the range funtion:

for i in range(1, 200, 2): #odd numbers
for i in range(0, 200, 2): #even numbers
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.