Tôi đang cố gắng chạy một ứng dụng nhiều quy trình đơn giản bằng Python. Chủ đề chính sinh ra từ 1 đến N quy trình và chờ cho đến khi chúng được xử lý xong. Các quá trình từng chạy một vòng lặp vô hạn, vì vậy họ có khả năng có thể chạy mãi mãi nếu không có sự gián đoạn người dùng, vì vậy tôi đưa vào một số mã để xử lý một KeyboardInterrupt:Lỗi đa xử lý Python lỗi "Lỗi trong atexit._run_exitfuncs"
#!/usr/bin/env python
import sys
import time
from multiprocessing import Process
def main():
# Set up inputs..
# Spawn processes
Proc(1).start()
Proc(2).start()
class Proc (Process):
def __init__ (self, procNum):
self.id = procNum
Process.__init__(self)
def run (self):
doneWork = False
while True:
try:
# Do work...
time.sleep(1)
sys.stdout.write('.')
if doneWork:
print "PROC#" + str(self.id) + " Done."
break
except KeyboardInterrupt:
print "User aborted."
sys.exit()
# Main Entry
if __name__=="__main__":
main()
Vấn đề là khi sử dụng tổ hợp phím CTRL-C để thoát ra, tôi gặp lỗi bổ sung ngay cả khi các quy trình dường như thoát ngay lập tức:
......User aborted.
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Python26\lib\atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "C:\Python26\lib\multiprocessing\util.py", line 281, in _exit_function
p.join()
File "C:\Python26\lib\multiprocessing\process.py", line 119, in join
res = self._popen.wait(timeout)
File "C:\Python26\lib\multiprocessing\forking.py", line 259, in wait
res = _subprocess.WaitForSingleObject(int(self._handle), msecs)
KeyboardInterrupt
Error in sys.exitfunc:
Traceback (most recent call last):
File "C:\Python26\lib\atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "C:\Python26\lib\multiprocessing\util.py", line 281, in _exit_function
p.join()
File "C:\Python26\lib\multiprocessing\process.py", line 119, in join
res = self._popen.wait(timeout)
File "C:\Python26\lib\multiprocessing\forking.py", line 259, in wait
res = _subprocess.WaitForSingleObject(int(self._handle), msecs)
KeyboardInterrupt
Tôi đang chạy Python 2.6 trên Windows. Nếu có cách tốt hơn để xử lý đa xử lý bằng Python, vui lòng cho tôi biết.
Nếu có thể, bạn có thể thêm ví dụ chạy được của giải pháp này không. – cmcginty