beebek
beebek

Reputation: 1949

lock class/file to avoid multiple access

Example:

test.py

class test_class(object):
    def display(self):
        print "Hello"

lock1.py

from test import test_class
from time import sleep
obj = test_class()
while True:
    obj.display()
    sleep(1)

lock2.py

from test import test_class
obj = test_class()
# Raise error if instance of test_class has been implemented before
try:
   obj.display()
except Exception as e:
   print e

What I need to do is lock (or anything) the class (or entire test.py) for file lock2.py if the object has already been initialized for that class before raising Error(or Expeption). I've simplied the example though the example might not look relevant.

I tried locking the file i.e. test.py using

http://packages.python.org/lockfile/lockfile.html

http://pypi.python.org/pypi/zc.lockfile

but it doesn't seem to help.


HERE IS A ACTUAL CODE SNIPPET

comm_port.py

import serial
class CommPort(object):
    def __init__(self):
        self.ser = serial.Serial("/dev/ttyUSB0")
        # update in db (I've removed actual db update process
        # db.flag = 1        

accessing_file1.py

from comm_port import CommPort
# if db.flag != 1:
    port = Commport()
    port.ser.flushInput()
    port.ser.flushOutput()
    ## will flush the buffer.. what if it flused the data that was supposed for go for accessing_file2
    port.ser.write("1")
    # do stuff using serial-port object "port"
    # lets say script gets busy for 30 secs for doing some stuffs
    # db.flag = 0

accessing_file2.py

from comm_port import CommPort
# if db.flag != 1:
    port = Commport()
    port.ser.flushInput()
    port.ser.flushOutput()
    port.ser.write("2")
    # do stuff using serial-port object "port"
    # lets say script gets busy for 40 secs for doing some stuffs
    # db.flag = 0

Still the example might not look relevant but this is my case. Both of file can be activated at once too but I need only one to operate at a time. what i did was created a db flag if comm_port.py is used and rest files check this flag. if comm_port is busy other accessing files will not work. But I dont think its best practice.

So I need to check if there is a way to check if CommPort class is being instiated by any object or by locking the comm_port.py or any other existing ideas are most welcome.

Upvotes: 0

Views: 254

Answers (2)

root
root

Reputation: 80346

You can also check if an instance already exists and raise an error if it does:

In [1]: import gc

In [2]: class Foo(object):
   ...:     pass

In [3]: bar=Foo()

In [4]: [item for item in gc.get_referrers(Foo) if isinstance(item,Foo)]
Out[4]: [<__main__.Foo at 0x187cc90>]

use any([isinstance(item,Foo) for item in gc.get_referrers(Foo)]), if True, raise an exeption.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798616

You can't apply a lock to this situation, since importing doesn't work this way. An imported module is only executed once, the first time it is imported. Subsequent imports only copy the existing reference from sys.modules. You will need to figure out what your actual problem is and ask about that instead.

Upvotes: 1

Related Questions