Skip to content

Commit

Permalink
[测试]单例模式
Browse files Browse the repository at this point in the history
  • Loading branch information
longfengpili committed Nov 20, 2021
1 parent bfe22fa commit b0442c6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
8 changes: 6 additions & 2 deletions pydbapi/api/mysql.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @Author: chunyang.xu
# @Email: [email protected]
# @Date: 2020-06-10 14:40:50
# @Last Modified time: 2021-11-16 18:42:55
# @Last Modified time: 2021-11-20 10:22:21
# @github: https://github.com/longfengpili

# !/usr/bin/env python3
Expand Down Expand Up @@ -114,18 +114,22 @@ def __init__(self, host, user, password, database, port=3306, charset="utf8", sa

def __new__(cls, *args, **kwargs):
if not hasattr(MysqlDB, '_instance'):
mysqllogger.info(MysqlDB._instance_lock)
with MysqlDB._instance_lock:
mysqllogger.info(MysqlDB._instance_lock)
if not hasattr(MysqlDB, '_instance'):
MysqlDB._instance = super().__new__(cls)

return MysqlDB._instance

@classmethod
def get_instance(cls, *args, **kwargs):
mysqllogger.info('1')
if not hasattr(MysqlDB, '_instance'):
mysqllogger.info(MysqlDB._instance_lock)
with MysqlDB._instance_lock:
if not hasattr(MysqlDB, '_instance'):
MysqlDB._instance = MysqlDB(*args, **kwargs)
MysqlDB._instance = super().__new__(cls)

return MysqlDB._instance

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @Author: chunyang.xu
# @Email: [email protected]
# @Date: 2020-06-09 16:46:54
# @Last Modified time: 2021-11-19 14:57:01
# @Last Modified time: 2021-11-20 10:22:31
# @github: https://github.com/longfengpili

# !/usr/bin/env python3
Expand All @@ -12,7 +12,7 @@
import shutil
import setuptools

VERSION = '0.0.78'
VERSION = '0.0.79'
PROJECT_NAME = 'pydbapi'

with open('README.md', 'r', encoding='utf-8') as f:
Expand Down
16 changes: 8 additions & 8 deletions tests/mysql/mysql_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: chunyang.xu
# @Date: 2021-03-08 14:19:01
# @Last Modified by: chunyang.xu
# @Last Modified time: 2021-11-16 18:41:11
# @Last Modified time: 2021-11-20 10:15:24

import os
import pytest
Expand Down Expand Up @@ -35,13 +35,13 @@ def teardown_method(self, method):

def test_get_instance(self):
mysql1 = MysqlDB.get_instance(safe_rule=False, **self.AdLocal)
mysql2 = MysqlDB.get_instance(safe_rule=False, **self.AdLocal)
mysql3 = MysqlDB(safe_rule=False, **self.AdLocal)
mysql4 = MysqlDB(safe_rule=False, **self.AdLocal)
print(mysql1, mysql2, mysql3, mysql4)
for i in dir(mysql4):
result = eval(f"mysql4.{i}")
print(f"【{i}】: {result}")
# mysql2 = MysqlDB.get_instance(safe_rule=False, **self.AdLocal)
# mysql3 = MysqlDB(safe_rule=False, **self.AdLocal)
# mysql4 = MysqlDB(safe_rule=False, **self.AdLocal)
# print(mysql1, mysql2, mysql3, mysql4)
# for i in dir(mysql4):
# result = eval(f"mysql4.{i}")
# print(f"【{i}】: {result}")

def test_drop(self):
rows, action, result = self.mysqldb.drop(self.tablename)
Expand Down

0 comments on commit b0442c6

Please sign in to comment.