Multiple Instantiation of Programs interacting with one another (Python2.7 & Tkinter)
Question: 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!


Answer: 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](http://beets.radbox.org/blog/sqlite-nightmare.html), 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.

