3

handler.py

class handler:

    cursor = None
    _instance= None
    db = MySQLdb.connect("localhost","root","root","db" )
    count=0

    @staticmethod
    def getInstance():
        if(handler._instance == None):
            handler._instance = handler()
            print "new"
        return handler._instance

    def __init__(self):
        handler.cursor = handler.db.cursor()
        print "ok"

obj1 = handler.getInstance()

obj2 = handler.getInstance() # both objects correpond to same single instance of class 'handler'.

If you call getInstance() function in two different files simultaneously to create object in each of them..it creates separate single instances. But I want the same single instance to be used in each of them so that any changes made by one program to that instance gets reflected in other program running simultaneously. For Example:

test1.py

import handler
import time
x = handler.handler.getInstance()

time.sleep(20)

test2.py

import handler

y = handler.handler.getInstance()

When I execute both files simultaneously, 'print "new"' statement gets executed twice, but I want it to be executed only once to ensure that only a single instance of class 'handler' gets created.

Relevant References (this is in Java and I tried to do in python....didn't work):

Creating one instance of an object and using the same instance in different class files

Community
  • 1
  • 1
Ayush
  • 33
  • 3
  • 2
    how are you running these two different files simultaneously? – Anand S Kumar Jun 23 '15 at 09:20
  • What you are looking for is the Singleton pattern. See http://stackoverflow.com/questions/31875/is-there-a-simple-elegant-way-to-define-singletons-in-python?lq=1 Short answer: use a module level variable as they are only imported once. – Kirell Jun 23 '15 at 09:29
  • @Anand: What I meant by running simultaneously is that while the first file is using the instance of class handler to connect to database, I run the second file before the delay specified in the first file times out to ensure that both are running at a specific time. – Ayush Jun 23 '15 at 11:38
  • Are both of them running using different python commands? If so, there is no way to share the instance, since they are completely different processes , you will have to use some kind of piping mechanism or such to communicate between processes – Anand S Kumar Jun 23 '15 at 12:06
  • @Kikohs:Yes, I was looking for singleton pattern which I have already implemented it in 'handler.py', but that doesn't solve my issue. I tried this: made the variable '_instance' global in 'handler.py' and changed the code appropriately in the two files and it did not work. Is that what you meant? or did I misunderstand you?..Thanks in advance! – Ayush Jun 23 '15 at 12:07

1 Answers1

0

If you want these scripts to share an instance then make a third script import these two scripts

maindir
    -->handler.py
    -->test1.py
    -->test2.py
    -->main.py

In main.py :

import handler
import test1, test2
x = handler.handler.getInstance()

#perform operations of test1

y = handler.handler.getInstance()
#this time handler already has already instantiated so it wont print new
#performOperations of test2
shaktimaan
  • 1,769
  • 13
  • 14
  • Thanks for your input! This works, I am looking to implement this in future if no other direct method exists, for now, this cannot be done as my requirement is different. :) – Ayush Jun 23 '15 at 12:47