Issue
I want to share a variable between multiple Python scripts that are open. One does not import another. Imagine this:
main1.py
#do something to share the dict that's about to be assigned at the bottom (optional)
#by that i mean i can still import it from a module or something else
main = {"apples":"are fine"}
print(main['apples'])
main['apples'] = "are delicious"
main2.py
#do something to get the dict from the first script or get the shared var
input()
#^^^ to wait until the first script changes the apples value and then access it manually
print(main['apples'])
>>> "are delicious"
This problem is based on the worker system that pythonanywhere provides to handle incoming requests quicker. But the thing is, it involves having multiple python flask scripts running at the same time and as I use the flask app to store data, I somehow need a way of sharing a dict between these two instances.
To rephrase that:
- I'm using a Flask app
- That unfortunately must have several instances of itself (scripts)
- I need data to be updated on both scripts so it can then be instantly accessed in a following request
- So I need a shared dict/module var between those two scripts
- Because what happens is that the first request updates something in the dict of the first script and when accessed again, the second script (worker) responds with completely different data
Solution
You could use pyro4 + pickle to share data between applications over network (localhost 127.0.0.1). https://docs.python.org/3/library/pickle.html https://pyro4.readthedocs.io
Answered By - phibel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.