Search code examples
pythonflaskuwsgiapscheduler

Object does not contain data but ONLY when used with apscheduler in uWSGI FLASK app


Hello I have build a Flask website that I have deployed with uWSGI.

Everything seems to work as expected, apart from my database update code that is run using apscheduler.

I have an object that essentially contains some data dictionaries that i want to update hourly using apscheduler.

When i try to access the object normally, everything works as expected. However when i try to use the object with an apscheduler BackgroundScheduler, the object does not contain any data, even though it is in the same memory location!!

The code that produces my output is:

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))

    ...

When executed within a flask page it yields:

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

However when called by an apscheduler job it gives:

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

Here the memory locations of the objects are identical as before! But the _content_dict contains an empty dict when called by the scheduler? (The data doesnt get deleted, as it is still there when i call it again normally afterwards.)

When I use flask's builtin development server the apscheduler update function works fine, but not with uWSGI.

The apscheduler is configured as follows:

# 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())

my uWSGI.ini is as follows:

[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

Can someone explain why the data isn't there Exclusively when it's called with apscheduler? Eventhough the object memory locations are identical?

Or alternatively how should I run an hourly database update function?


Solution

  • I am not sure yet why, but adding:

    lazy-apps = true
    

    to the uWSGI.ini file, fixed the problem