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
757 views
in Technique[技术] by (71.8m points)

multithreading - Python: How to NOT wait for a thread to finish to carry on?

So I have some code that waits for X to happen, then creates a thread and does processEmail.

What I am looking for is a way for the code to carry on waiting X even though processEmail is happening in another thread but currently the code just waits for the thread to finish before waiting for X to happen again.

if X happens:
    thread = Thread(target = processEmail.main())
    thread.start()

EDIT: FYI I have nothing that requires the output of processEmail.main() further down the code therefore there is no need for me to wait for its output.

ANSWER Provided by Jean: Remove the () after main.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Problem is that you're actually calling your method when passing it as argument of Thread.

So it executes, but in the current thread, that's why it's working but it's blocking (and since it probably returns None, you get no error from the Thread object, it just blocks)

Remove parentheses to pass the function object, not the result from the call!

thread = Thread(target = processEmail.main)
thread.start()

Note: some IDEs like PyCharm automatically add parentheses to function names. That's a bad idea in that case :)


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

...