2017-03-19 13 views
0

webpyを使用して、非常に重い初期化段階を持つ2つの関数を持つWebサービスをホストしています。 test_server.pyにはwebpyのmain関数があり、test_classAとtest_classBには2つの関数の主な実装が含まれています。私は、アプリケーションをテストするためにカールを使用Pythonクラスをwebpyで一度だけ初期化する

# test_server.py 

import web 
from test_classA import classA 
from test_classB import classB 

urls = (
    '/clsa', 'clsa', 
    '/clsb', 'clsb', 
) 

class clsa: 
    ca = classA('subtype1') 

    def __init__(self): 
     self.ca.dosomething('subtype1') 

    def POST(self): 
     self.ca.doanotherthing() 

class clsb: 
    cb = classB() 

    def POST(self): 
     self.cb.dosomething() 


if __name__ == "__main__": 
    app = web.application(urls, globals()) 
    app.run() 

# test_classA.py 

class classA(): 
    print "init class A" 
    varA = {} 

    def __init__(self, subtype): 
     """a very heavy-loaded function that read a lot of data into memory""" 
     print "init class A instance, subtype:", subtype 

    def dosomething(self, subtype): 
     print "class A do something for " + subtype 
     self.varA['apple'] = 1 

    def doanotherthing(self): 
     print "class A do another thing" 
     self.varA['orange'] = 2 
     print self.varA 

# test_classB.py 

class classB(): 
    print "init class B" 
    varB = {} 

    def __init__(self): 
     """a very heavy-loaded function that read a lot of data into memory""" 
     print "init class B instance" 

    def dosomething(self): 
     print "class B do something" 

...

$ curl localhost:8084/clsb --data-binary "hello" 
None$ curl localhost:8084/clsa --data-binary "hello" 
None$ curl localhost:8084/clsb --data-binary "hello" 

そして、私は私の質問は、私は一度だけクラスAとClassBのを初期化することができますどのように、ある

$ python test_server.py 8084 
init class A 
init class B 
init class A instance, subtype: subtype1 
init class B instance 
http://0.0.0.0:8084/ 
init class A instance, subtype: subtype1 
init class B instance 
class B do something 
127.0.0.1:50760 - - [19/Mar/2017 20:03:08] "HTTP/1.1 POST /clsb" - 200 OK 
class A do something for subtype1 
class A do another thing 
{'orange': 2, 'apple': 1} 
127.0.0.1:50780 - - [19/Mar/2017 20:03:17] "HTTP/1.1 POST /clsa" - 200 OK 
class B do something 
127.0.0.1:50786 - - [19/Mar/2017 20:03:32] "HTTP/1.1 POST /clsb" - 200 OK 

...カールコマンドで次を取得します。つまり、私はあなたが欲しいのはsingleton classある

>>> init class A instance, subtype: subtype1 
>>> init class B instance 

答えて

0

..送られる第1または第2のカールコマンドの間に、一度だけ、次のログを取得します。クラスのインスタンス化を1つのオブジェクトに制限するクラス。

Pythonには、シングルトンクラスの組み込みサポートがありません。ただし、デコレータを使用して模倣することはできます。

0

モジュールは事実上、シングルトンと似ています。

class classA(): 
    print "init class A" 
    varA = {} 

    def __init__(self, subtype): 
     ...etc... 

A = classA() # <-- Add this 

はtest_classB.pyについて同様の操作を行います。これを行う:test_classA.pyで

は、クラスAのインスタンス化を追加します。

次に、あなたのtest_server.pyではなくクラスよりも、インスタンスに引っ張って、あなたのインポートを変更します。

from test_classA import A 
from test_classB import B 

そして、あなたのclsa内およびclsbの操作を行います。

class clsa: 
    ca = A 
    .... 

class clsb: 
    cb = B 
    .... 

そうすれば、クラスはモジュールによって一度初期化されます。

関連する問題