這個問題在這里已經有了答案: 每個 Flask 會話存盤大資料或服務連接 1 個答案 3天前關閉。
我想在打開的多個 Python 腳本之間共享一個變數。一個不匯入另一個。想象一下:
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"
此問題基于 pythonanywhere 提供的用于更快處理傳入請求的作業系統。但問題是,它涉及同時運行多個 python 燒瓶腳本,并且當我使用燒瓶應用程式存盤資料時,我不知何故需要一種在這兩個實體之間共享字典的方法。
換個說法:
- 我正在使用 Flask 應用程式
- 不幸的是,它必須有幾個自身的實體(腳本)
- 我需要在兩個腳本上更新資料,以便可以在以下請求中立即訪問它
- 所以我需要這兩個腳本之間的共享 dict/module var
- 因為發生的情況是第一個請求更新了第一個腳本的 dict 中的某些內容,當再次訪問時,第二個腳本(worker)以完全不同的資料回應
uj5u.com熱心網友回復:
您可以使用 pyro4 pickle 通過網路(本地主機 127.0.0.1)在應用程式之間共享資料。 https://docs.python.org/3/library/pickle.html https://pyro4.readthedocs.io
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/488519.html
