Multiple Instantiation of Programs interacting with one another (Python2.7 & Tkinter) Multiple Instantiation of Programs interacting with one another (Python2.7 & Tkinter) tkinter tkinter

Multiple Instantiation of Programs interacting with one another (Python2.7 & Tkinter)


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 liteimport timecon = 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.