Note that there are some explanatory texts on larger screens.

plurals
  1. POlock class/file to avoid multiple access
    primarykey
    data
    text
    <p>Example:</p> <p>test.py</p> <pre><code>class test_class(object): def display(self): print "Hello" </code></pre> <p>lock1.py</p> <pre><code>from test import test_class from time import sleep obj = test_class() while True: obj.display() sleep(1) </code></pre> <p>lock2.py</p> <pre><code>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 </code></pre> <p>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.</p> <p>I tried locking the file i.e. test.py using </p> <pre><code>http://packages.python.org/lockfile/lockfile.html http://pypi.python.org/pypi/zc.lockfile </code></pre> <p>but it doesn't seem to help.</p> <hr> <h2>HERE IS A ACTUAL CODE SNIPPET</h2> <p>comm_port.py</p> <pre><code>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 </code></pre> <p>accessing_file1.py</p> <pre><code>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 </code></pre> <p>accessing_file2.py</p> <pre><code>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 </code></pre> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload