2

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. (Sorry for my poor English)

Let's say:

  • Five Users have opened this program at the same time using same PC. (Hypothetical)
  • There are three functions in the program, namely Order, See Inventory ,and See 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. (Cannot edit single txt file at once)

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. 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!

user3404844
  • 145
  • 2
  • 3
  • 14
  • Have a look at `multiprocessing` and Proxies in particular. Also you may want to look at Pyro. Here is an answer that may guide you: http://stackoverflow.com/questions/21642880/python-implementing-simple-web-data-storage/21644326#21644326 – User Mar 19 '14 at 12:55
  • @User Thank you for sharing the link! But I wanted to know how to do this without the aid of server :) – user3404844 Mar 19 '14 at 13:07
  • 1
    Of cause you can. Here is a paper about how you would use no server: [Paxos Made Simple](http://research.microsoft.com/en-us/um/people/lamport/pubs/pubs.html#paxos-simple) You are then in the context of distributed, fault-tolerant systems, I guess. I like serverless peer-to-peer solutions but they seem difficult to get right. That is the reason why many people use a server. – User Mar 19 '14 at 13:51
  • @User That is a wonderful information! Thank you so much! – user3404844 Mar 19 '14 at 14:37

1 Answers1

0

You could use the sqlite3 module to create a light-weight database. This does not need a server program; the database manager is in the Python standard library. Multiple instances would read/write to the same file database and sqlite would take care of ensuring consistency.

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.

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
  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.
logc
  • 3,813
  • 1
  • 18
  • 29