Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

python threading blocks

I am trying to write a program which creates new threads in a loop, and doesn't wait for them to finish. As i understand it if i use .start() on the thread, my main loop should just continue, and the other thread will go off and do its work at the same time

However once my new thread starts, the loop blocks until the thread completes. Have I misunderstood how threading works in python, or is there something stupid I'm doing.

here is my code for creating new threads.

def MainLoop():
    print 'started'
    while 1:
        if not workQ.empty():
            newThread = threading.Thread(target=DoWorkItem(), args=())
            newThread.daemon = True
            newThread.start()
        else:
            print 'queue empty'

thanks all

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This calls the function and passes its result as target:

threading.Thread(target=DoWorkItem(), args=())

Lose the parentheses to pass the function object itself:

threading.Thread(target=DoWorkItem, args=())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...