[英]Multiple Instantiation of Programs interacting with one another (Python2.7 & Tkinter)
I currently have a restaurant simulation program, GUI by Tkinter and I was finding a way to allow multiple instantiation of the programs to interact with one another in the sense of updating its key variables. 我目前有一个餐厅模拟程序,Tkinter的GUI,我正在寻找一种方法,可以在更新其关键变量的意义上使程序的多个实例相互交互。 (Sorry for my poor English)
(对不起,我英语不好)
Let's say: 比方说:
Order
, See Inventory
,and See Sales
Order
, See Inventory
和See Sales
Now, the restaurant must keep track of the orders made, and the inventory to cook the food. 现在,餐厅必须跟踪所下的订单以及烹饪食物的库存。
I tried coding the program by using txt
file import and export to keep track of the data but it was just now that I realized the txt
file can be edited by programs ONE AT THE TIME. 我尝试通过使用
txt
文件导入和导出来跟踪程序来对程序进行编码,以跟踪数据,但是直到现在,我才意识到txt
文件可以一次由程序进行编辑。 (Cannot edit single txt
file at once) (无法一次编辑单个
txt
文件)
Q: What is the better approach in saving the values of the variables used in the program so that multiple users can use the program with real-time updated values of the variables? 问:保存程序中使用的变量的值,以便多个用户可以实时使用变量的更新值使用该程序,哪种方法更好?
Global
variables. Global
变量。 But how could I possible assign values to them and keep them updated? Thank you for reading my Question! 感谢您阅读我的问题!
You could use the sqlite3
module to create a light-weight database. 您可以使用
sqlite3
模块创建轻量级数据库。 This does not need a server program; 这不需要服务器程序。 the database manager is in the Python standard library.
数据库管理器位于Python标准库中。 Multiple instances would read/write to the same file database and sqlite would take care of ensuring consistency.
多个实例将读取/写入同一文件数据库,而sqlite将确保一致性。
However, please note that there is a 5-second global lock on most sqlite implementations , so any of your multiple instances must complete its read/write in less than that time or it will cause a 'database locked' exception in the other instances. 但是,请注意, 大多数sqlite实现都具有5秒的全局锁定 ,因此您的多个实例中的任何一个实例都必须在少于该时间的时间内完成其读/写操作,否则在其他实例中将导致“数据库锁定”异常。
Here you have an example: 这里有一个例子:
import sqlite3 as lite
import time
con = lite.connect('common.db')
cur = con.cursor()
cur.execute(
"CREATE TABLE IF NOT EXISTS restaurant (orderId INT primary key, inventory TEXT, sales INT);")
for i in range(5):
print "About to insert on ID: %s" % i
cur.execute("INSERT INTO restaurant VALUES(%d, 'burger', 1)" % i)
time.sleep(1)
con.close()
If you execute this code on two terminals at the same time, you will notice: 如果您同时在两个终端上执行此代码,则会注意到:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.