I have a python script that works with threads, processes, and connections to a database. When I run my script, python crashes.
I cannot explicitly detect the case in which this happens.
Now I am looking for tools to get more information when python crashes, or a viewer to see all my created processes/connections.
我创建了一个模块RemoteException.py,它显示进程中异常的完整回溯。Python2。下载它并将其添加到您的代码中:
import RemoteException
@RemoteException.showError
def go():
raise Exception('Error!')
if __name__ == '__main__':
import multiprocessing
p = multiprocessing.Pool(processes = 1)
r = p.apply(go) # full traceback is shown here
Run Code Online (Sandbox Code Playgroud)
旧答案
我也有这个问题。
这就是我所做的...用于调试多处理调用的 RemoteException
复制源代码并删除第 19 行:
file.write('\nin %s ' % (Process.thisProcess,))
和行
import Process
问题是:多处理仅传输异常,但丢失了回溯。下面的代码创建一个保存回溯的 Exception 对象。并在调用过程中打印它。
在你的脚本中你可以做这样的事情:
import RemoteException
def f():
try:
# here is code that fails but you do know not where
pass
except:
ty, err, tb = RemoteException.exc_info() # like sys.exc_info but with better message
raise ty, err, tb
# here follows your multiprocessing call to f
Run Code Online (Sandbox Code Playgroud)
pip install celery
Run Code Online (Sandbox Code Playgroud)
然后在你的代码中:
from celery.contrib import rdb; rdb.set_trace()
Run Code Online (Sandbox Code Playgroud)
这就像远程 pdb 一样工作,如下所示:
Remote Debugger:6899: Ready to connect: telnet 127.0.0.1 6899
Type `exit` in session to continue.
Remote Debugger:6899: Waiting for client...
Run Code Online (Sandbox Code Playgroud)
从另一个窗口telnet localhost 6899,您就可以使用完整功能的pdb。