我是python中的新手。我很感激任何帮助。所以,让我们开始吧。
我正在运行一个FLASK apllication作为rest api。每次请求都会返回一个json。每秒可能有大约20个请求。在后台,我使用APscheduler每隔60秒从ldap获取实际数据。
scheduler = BackgroundScheduler()
scheduler.add_job(
func=my_ldap.fetch_people_ldap,
trigger=IntervalTrigger(seconds=60),
id='fetching_data_job',
name='Fetch data from ldap every 60 seconds')
scheduler.start()
atexit.register(lambda : scheduler.shutdown())
但实际上,当使用api调用点击datafetch时,应用程序正在关闭导致内存问题的原因,我认为这是导致我在访问ldap对象的同时fetch_people_ldap
- 函数是由调度程序调用。
我想通过阻止处理api调用的线程来解决这个可怕的错误,直到ldap数据获取成功退出为止。但我不知道该怎么做。
有任何建议或消息吗?
这是我得到的错误日志: 致命的Python错误:保存线程两次?
Thread 0x00007f95f3fff700 (most recent call first):
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 294 in _ldap_call
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 721 in result4
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 714 in result3
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 707 in result2
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 703 in result
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 796 in search_ext_s
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 802 in search_s
File "/path/to/folder/tt_report_api/training_tool_report_api/sample/ldap_check/ldapCheck.py", line 49 in fetch_people_ldap
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/apscheduler/executors/base.py", line 125 in run_job
File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55 in run
File "/usr/lib/python3.5/concurrent/futures/thread.py", line 66 in _worker
File "/usr/lib/python3.5/threading.py", line 862 in run
File "/usr/lib/python3.5/threading.py", line 914 in _bootstrap_inner
File "/usr/lib/python3.5/threading.py", line 882 in _bootstrap
Thread 0x00007f95f8910700 (most recent call first):
File "/usr/lib/python3.5/threading.py", line 297 in wait
File "/usr/lib/python3.5/threading.py", line 549 in wait
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/apscheduler/schedulers/blocking.py", line 28 in _main_loop
File "/usr/lib/python3.5/threading.py", line 862 in run
File "/usr/lib/python3.5/threading.py", line 914 in _bootstrap_inner
File "/usr/lib/python3.5/threading.py", line 882 in _bootstrap
Current thread 0x00007f9605505700 (most recent call first):
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 294 in _ldap_call
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 791 in search_ext
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 795 in search_ext_s
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 802 in search_s
File "/path/to/folder/tt_report_api/training_tool_report_api/sample/ldap_check/ldapCheck.py", line 59 in check_node
File "/path/to/folder/tt_report_api/training_tool_report_api/sample/ldap_check/ldapCheck.py", line 118 in build_tree_recursive
File "/path/to/folder/tt_report_api/training_tool_report_api/sample/ldap_check/ldapCheck.py", line 124 in build_tree_recursive
File "/path/to/folder/tt_report_api/training_tool_report_api/sample/ldap_check/ldapCheck.py", line 124 in build_tree_recursive
File "/path/to/folder/tt_report_api/training_tool_report_api/sample/ldap_check/ldapCheck.py", line 129 in build_tree
File "/path/to/folder/tt_report_api/training_tool_report_api/sample/app/app.py", line 83 in get_trainings_by_unit
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/flask/app.py", line 1598 in dispatch_request
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/flask/app.py", line 1612 in full_dispatch_request
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/flask/app.py", line 1982 in wsgi_app
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/flask/app.py", line 1997 in __call__
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/werkzeug/serving.py", line 197 in execute
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/werkzeug/serving.py", line 209 in run_wsgi
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/werkzeug/serving.py", line 267 in handle_one_request
File "/usr/lib/python3.5/http/server.py", line 422 in handle
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/werkzeug/serving.py", line 232 in handle
File "/usr/lib/python3.5/socketserver.py", line 681 in __init__
File "/usr/lib/python3.5/socketserver.py", line 354 in finish_request
File "/usr/lib/python3.5/socketserver.py", line 341 in process_request
File "/usr/lib/python3.5/socketserver.py", line 313 in _handle_request_noblock
File "/usr/lib/python3.5/socketserver.py", line 234 in serve_forever
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/werkzeug/serving.py", line 539 in serve_forever
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/werkzeug/serving.py", line 702 in inner
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/werkzeug/serving.py", line 739 in run_simple
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/flask/app.py", line 841 in run
File "/path/to/folder/tt_report_api/training_tool_report_api/sample/app/app.py", line 92 in <module>
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
答案 0 :(得分:1)
问题是你的API调用处理程序也在调用LDAP来创建响应。如果您每秒钟要获得20个请求,那么这会导致问题。预定呼叫也没有多大意义,因为您何时使用检索到的数据?
我的方法是让计划任务在后台更新公共数据存储。然后,您的API调用仅从该公共数据存储中读取数据,而根本不需要触摸LDAP。无论您使用数据库还是仅使用内存,都取决于您正在使用的数据的大小和复杂程度。
主要是将创建API响应与从LDAP中提取数据分离。
修改强> 我从您的评论中了解到您认为您的API仅使用您自己的ldapCheck.py代码,但如果您查看完整的堆栈跟踪,它实际上是使用LDAP库进行调用:
API线程:
Current thread 0x00007f9605505700 (most recent call first):
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 294 in _ldap_call
...
File "/path/to/folder/tt_report_api/training_tool_report_api/sample/ldap_check/ldapCheck.py", line 124 in build_tree_recursive
...
File "/path/to/folder/tt_report_api/training_tool_report_api/sample/app/app.py", line 83 in get_trainings_by_unit
...
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/flask/app.py", line 841 in run
如果在您的预定后台线程调用LDAP的同时发生这种情况,则会出现此错误。
Thread 0x00007f95f3fff700 (most recent call first):
File "/path/to/folder/virtual_env_1/lib/python3.5/site-packages/pyldap-2.4.37-py3.5-linux-x86_64.egg/ldap/ldapobject.py", line 294 in _ldap_call
...
File "/path/to/folder/tt_report_api/training_tool_report_api/sample/ldap_check/ldapCheck.py", line 49 in fetch_people_ldap
...
File "/usr/lib/python3.5/threading.py", line 882 in _bootstrap
解决方案不是阻止您的API线程。解决方案是确保ldapCheck.py", line 59 in check_node
中的代码不会调用您的LDAP库,而是使用已经由后台线程检索和存储的信息。