问题描述
除了我使用apscheduler运行的数据库更新代码外,一切似乎都能按预期工作。
我有一个基本上包含一些数据字典的对象,我想使用apscheduler每小时更新一次。
当我尝试正常访问该对象时,一切都按预期工作。 但是,当我尝试通过apscheduler BackgroundScheduler使用该对象时,该对象不包含任何数据,即使它位于相同的内存位置中!
def update():
print("hex id of object: ",hex(id(_ESI_data)))
print("hex id of object.prices: ",hex(id(_ESI_data.prices)))
print("hex id of object.prices._content_dict: ",hex(id(_ESI_data.prices._content_dict)))
print("_content_dict: ",_ESI_data.prices._content_dict)
print("type: ",type(_ESI_data))
print('prices length: ',len(_ESI_data.prices))
...
在烧瓶页面中执行时,将产生:
hex id of object: 0x7f58a5e88a60
hex id of object.prices: 0x7f58a5e88ac0
hex id of object.prices._content_dict: 0x7f58ab0a8180
_content_dict: {625: (12925714.285714285,9044000.0),34: (8.528115645081806,8.0),35: (13.499491140271743,35.0),36: (109.86576894948205,113.1),37: (37.98743083746043,42.64),38: (1311.6629430032253,1225.0),39: (1347.7675049393822,1354.0),40: (808.3051410710929,787.0)}
type: <class 'app.EVE_interfaces.EVE_ESI_interface.ESI_data_obj'>
prices length: 8
然而,当由apscheduler作业调用时,它会给出:
hex id of object: 0x7f58a5e88a60
hex id of object.prices: 0x7f58a5e88ac0
hex id of object.prices._content_dict: 0x7f58ab0a8180
_content_dict: {}
type: <class 'app.EVE_interfaces.EVE_ESI_interface.ESI_data_obj'>
prices length: 0
这里对象的内存位置与以前一样!!但是 _content_dict由调度程序调用时是否包含空dict ? (数据不会被删除,因为当我以后再次正常调用它时,它仍然存在。)
当我使用flask的内置开发服务器时,apscheduler更新功能可以正常运行,但不适用于uWsgi。
apscheduler的配置如下:
# run update once at the start
update()
# set up scheduler to repeatedly update ESI data
scheduler = BackgroundScheduler()
scheduler.add_job(func=update,trigger='interval',minutes=_ESI_update_interval)
scheduler.start()
# Shut down the scheduler when exiting the app
atexit.register(lambda: scheduler.shutdown())
我的uWsgi.ini如下:
[uwsgi]
module = main:flask_app
master = true
processes = 1
enable-threads = true
single-interpreter = true
socket = web_app.sock
chmod-socket = 660
vacuum = true
die-on-term = true
logto = /home/mainuser/web_app/uwsgi_logs/%n.log
有人可以解释为什么用apscheduler调用数据时不存在数据吗?尽管对象存储位置相同?
解决方法
我不确定为什么,但是添加:
lazy-apps = true
修复uWSGI.ini文件