更新:在dano的幫助下,我解決了這個問題。如何在多處理進程完成後退出python腳本?
我沒有調用生產者join()
,它使我的腳本掛起。 只需要添加一個行達諾說:
...
producer = multiprocessing.Process(target=produce,args=(file_queue,row_queue))
producer.daemon = True
producer.start()
...
舊腳本:
import multiprocessing
import Queue
QUEUE_SIZE = 2000
def produce(file_queue, row_queue,):
while not file_queue.empty():
src_file = file_queue.get()
zip_reader = gzip.open(src_file, 'rb')
try:
csv_reader = csv.reader(zip_reader, delimiter=SDP_DELIMITER)
for row in csv_reader:
new_row = process_sdp_row(row)
if new_row:
row_queue.put(new_row)
finally:
zip_reader.close()
def consume(row_queue):
'''processes all rows, once queue is empty, break the infinit loop'''
while True:
try:
# takes a row from queue and process it
pass
except multiprocessing.TimeoutError as toe:
print "timeout, all rows have been processed, quit."
break
except Queue.Empty:
print "all rows have been processed, quit."
break
except Exception as e:
print "critical error"
print e
break
def main(args):
file_queue = multiprocessing.Queue()
row_queue = multiprocessing.Queue(QUEUE_SIZE)
file_queue.put(file1)
file_queue.put(file2)
file_queue.put(file3)
# starts 3 producers
for i in xrange(4):
producer = multiprocessing.Process(target=produce,args=(file_queue,row_queue))
producer.start()
# starts 1 consumer
consumer = multiprocessing.Process(target=consume,args=(row_queue,))
consumer.start()
# blocks main thread until consumer process finished
consumer.join()
# prints statistics results after consumer is done
sys.exit(0)
if __name__ == "__main__":
main(sys.argv[1:])
目的:
我使用python 2.7
multiprocessing
生成3個生產者讀取3個文件的同時,然後將文件行放入row_queue
中,並生成1位消費者對所有行執行更多處理。打印統計結果在消費者完成後在主線程中產生,所以我使用join()
方法。最後調用sys.exit(0)
來退出腳本。
問題: 無法退出腳本。
我試圖用print "the end"
代替sys.exit(0)
,在控制檯上顯示「結束」。難道我做錯了什麼?爲什麼腳本不會退出,以及如何讓它退出?由於
這裏沒有足夠的信息可以肯定地說,但最可能的問題是一個或多個'Producer'進程仍在運行。嘗試通過在調用'producer之前添加'producer.daemon = True'來使它們守護進程。start()',看腳本是否會退出。 – dano 2015-04-02 16:35:11
或者,爲每個生產者調用'join()'以及...假設它們通常會終止。 – twalberg 2015-04-02 16:40:13
@twalberg如果它們最終結束,腳本將不需要調用'join()'而退出。當解釋器退出時,Python隱式地在非守護進程子進程上調用join()。 – dano 2015-04-02 16:41:34