Note1: I want to use mutiprocessing.Queue in multiple process, When I found this issue occurs in single-process situation. So, the following code is use single process to simplify question.
There is a similar question : Broken pipe error with multiprocessing.Queue.
The answer in that post proves that this issue is because the main thread exits before the queue thread finishes its job. The way he fixed it was by adding sleep(0.1)
to his code:
import multiprocessing
import time
def main():
q = multiprocessing.Queue()
for i in range(10):
print i
q.put(i)
time.sleep(0.1) # Just enough to let the Queue finish
if __name__ == "__main__":
main()
But, I think sleep is not a stable method for production code, so I tried to use join
to do that. You can see my code below, but unfortunately, it does not work. Is there someone who knows how to do this without sleep?
import multiprocessing
import time
def main():
q = multiprocessing.Queue()
for i in range(10):
q.put(i)
# time.sleep(4)
q.close()
q.join_thread()
if __name__ == "__main__":
main()