简体   繁体   English

程序相互交互的多个实例化(Python2.7和Tkinter)

[英]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: 比方说:

  • Five Users have opened this program at the same time using same PC. 五名用户使用同一台PC同时打开了该程序。 (Hypothetical) (假想)
  • There are three functions in the program, namely Order , See Inventory ,and See Sales 程序中包含三个功能,分别是OrderSee InventorySee Sales
  • Assume that all five have made an order 假设所有五个人都下了订单

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? 问:保存程序中使用的变量的值,以便多个用户可以实时使用变量的更新值使用该程序,哪种方法更好?

  • Someone told me that I could try using Global variables. 有人告诉我,我可以尝试使用Global变量。 But how could I possible assign values to them and keep them updated? 但是我怎么可能给它们赋值并保持更新呢? (Local variables get initialized everytime when the program ran) (每次运行程序时都会初始化局部变量)

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: 如果您同时在两个终端上执行此代码,则会注意到:

  1. a 'common.db' file is created 创建一个“ common.db”文件
  2. one of both executions advances freely; 两次处决之一自由推进; the other advances to the point "About to insert on ID: 0" and then gets locked until the first one is completed. 另一个前进到“要在ID上插入:0”这一点,然后锁定直到第一个完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM