0

I am new to threading and queuing. the below function just takes a string and returns a list of the truncated char ex: 789 it returns ['789','89','9'].but I am getting weird output.

def do_stuff(q):

    while True:
        x=str(q.get())
        print [x[i:]for i in range(len(x))]   
        q.task_done()

q = Queue(maxsize=0)
num_threads = 10

for i in range(num_threads):
  worker = threading.Thread(target=do_stuff, args=(q,))
  worker.setDaemon(True)
  worker.start()

for x in xrange(10,100000):
  q.put(x)

q.join()

myoutput:

 ['10', ['0''1]1'[
    , '12''1', ]'2'
     ][
    '13'[, '1'43''[, ]'15''4'
    , ]['5'
    '16'], 
    '6' ][[
    '17''18', , '7''8'[]]'19'

    , '9']
    ['20'[, '21''0'[, ]'22''1'
    , ]['2'
    '23' ], [
    '3''24'][, 
    '25''4'[, ]'26''5'
    , ]['6'
    '27'][, 
    '28''7', ]'8'
     ][
    '29', '9']
    ['30', '0']
     [['31''32', , '1'['2']'33']
     , 
    ['3''34'], 
     '4'[]'35'
     , [['5''36''37'], , 
    '6''7'][]
    '38'
    , '8'[]'39
    ', '9']
    ['40'[, '41''0', ]'1'
     ][
    '42'[, '43''2'[, ]'44''3'
    , ]'4'
    ]['45'
    , '5'[]'46'
    , '6'[]'47'
    , '7'][
    '48', '8']
     ['49', '9']
    ['50'[, '51''0'[, ]'52''1'
    , ]'2'
    ]
     ['53', ['3''54'], 
     '4'[[]'55''56'
    , , '5''6'][]
    '57
    ', '7'] ..... so on

but expected output:

['10','0']
['11','1']
['12','1'] so on

Why is the queue overridden?

K DawG
  • 13,287
  • 9
  • 35
  • 66
sundar nataraj
  • 8,524
  • 2
  • 34
  • 46
  • 1
    It looks like your threads are just stepping on each other as they write to stdout. If you put a lock around the print statement I think you'll find the output closer to what you expect. – dano Jun 10 '14 at 04:57
  • @dano can u guide me how to put lock ..i am new to threading – sundar nataraj Jun 10 '14 at 04:58
  • @sundarnatarajサンダーナタラジ: It provides an answer, and a link to another question with more discussion. – nneonneo Jun 10 '14 at 05:26

1 Answers1

0

The Problem as Dano said the thread was overiding the queue. to solve it i searched for lock. i found the way

queueLock = threading.Lock()

def do_stuff(q):
    print threading.current_thread()
    while True:
        queueLock.acquire()
        x=str(q.get())
        print [x[i:]for i in range(len(x))]
        queueLock.release()    
        q.task_done()

now output is

['10', '0']
['11', '1']
['12', '2']
['13', '3']
['14', '4']...

thanks Dano

sundar nataraj
  • 8,524
  • 2
  • 34
  • 46