Skip to content
Snippets Groups Projects
Commit 91462a39 authored by Anton Gusev's avatar Anton Gusev
Browse files

change Connector for use singleton

parent 58195424
No related branches found
No related tags found
5 merge requests!5Develop,!4Develop,!3Develop,!2Develop,!1Develop
......@@ -3,17 +3,25 @@ from sqlalchemy.orm import sessionmaker
from scrapy.conf import settings
class Connector:
connection = None
engine = create_engine(settings['CONNECTION_STRING'])
Session = sessionmaker(bind=engine)
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Connector(metaclass=Singleton):
__connection = None
__engine = create_engine(settings['CONNECTION_STRING'])
__Session = sessionmaker(bind=__engine)
@staticmethod
def get_session():
return Connector.Session()
return Connector.__Session()
@staticmethod
def get_connection():
if not Connector.connection:
Connector.connection = Connector.engine.connect()
return Connector.connection
if not Connector.__connection:
Connector.__connection = Connector.__engine.connect()
return Connector.__connection
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment