def run(self): """ 1、获取该库房的管理模式; 2、根据模式选择相应的处理方法并循环执行; 3、数据无变化相隔interval秒后自动结束当前用户借还流程; 4、点亮相应的LCD; :return: """ self.gravity_precision = conpar.read_yaml_file('configuration')['gravity_precision'] self.interval = conpar.read_yaml_file('configuration')['storeroom_close_delay_time'] self._get_manage_mode() self._set_current_user(entrance_id=self.entrance_id, entrance_addr=self.addr) over_timer = threading.Timer(interval=self.interval, function=self._check_timeout_to_close, args=[self.interval, ]) over_timer.daemon = True over_timer.start() self._set_lcd_on() while self.isrunning: if not self.queue_pkg.empty(): with self.lock: self.is_close = False pkg = self.queue_pkg.get() if self.manage_mode == 1: self._handle_mode_one(pkg=pkg) elif self.manage_mode == 3: self._handle_mode_three(pkg=pkg) else: pass else: pass
def run(self): """ 1、主动连接所有已注册服务端; 2、监听等待连接所有已注册客户端; 3、打开注册子线程; 4、打开创建与监测子线程; 5、打开交互接口。 :return: """ try: print('PID--gateway_server:', os.getpid()) mylogger.info('PID--gateway_server: %d' % os.getpid()) # connect all servers if self.servers is not None: print("Start to connect to registered servers!!!!") for k, v in self.servers.items(): self._connect_server(addr=k, ttype=v[0], storeroom_id=v[1], uuid=v[2]) else: mylogger.info( 'There is None registered server for connecting!') # monitor and reconn servers thread_reconn_server = threading.Thread( target=self._reconnect_offline_server) thread_reconn_server.daemon = True thread_reconn_server.start() # listen all access clients thread_monitor_client = threading.Thread( target=self._monitor_access) thread_monitor_client.daemon = True thread_monitor_client.start() # monitor status subthread on time self.check_interval = conpar.read_yaml_file( 'configuration')['gateway_server_check_interval'] t = threading.Timer(interval=self.check_interval, function=self._thread_ontime) t.daemon = True t.start() # wait for cmd print('gateway server was start') while self.isrunning: self._handle_cmd() time.sleep(1) print('gateway server was stop') mylogger.error('gateway server was stop') except Exception as e: print('gateway server was stop by exception: ', e) mylogger.error('gateway server was stop by exception:' + e) print('\033[1;33m', 'gateway_server was stop', '\033[0m') mylogger.error('gateway_server was stop')
def run(self): """ 1、主线程负责外部指令处理以及接收处理,子线程负责数据发送; 2、使用生产消费者模式; 3、每隔5s从设备读取一次数据更新; :return: """ self.stop_running() self.tcp_socket.settimeout(1) thd_send = threading.Thread(target=self._send_recv) # thd_auto_inventory = threading.Timer(interval=5, function=self._inventory_once) thd_send.daemon = True thd_send.start() # thd_auto_inventory.start() # self._inventory() second_interval = conpar.read_yaml_file( 'configuration')['cm_r2000fh_update_interval'] self.update_interval = second_interval if second_interval > 5 else 5 thread_ontime = threading.Timer(interval=self.update_interval, function=self._check_data_update) thread_ontime.daemon = True thread_ontime.start() self.stop_running() while self.isrunning: try: # if not self.queuetask.empty(): # task, args = self.queuetask.get() # rsl = methodcaller(task, *args)(self) # if rsl is not None: # pkg = TransferPackage(code=SUCCESS, eq_type=4, data={'rsl': rsl}, source=self.addr, msg_type=4, # storeroom_id=self.storeroom_id, eq_id=self.uuid) # self.queuersl.put(pkg) # self.event.set() # else: # time.sleep(1) time.sleep(1) self.start_running() time.sleep(30) self.stop_running() break except KeyboardInterrupt: self._stop() self.tcp_socket.shutdown(2) self.tcp_socket.close() print('thread CM_R2000FH is closed.....')
def __init__(self): CONF = conpar.read_yaml_file('mysql') self.__db = pymysql.connect(CONF['host'], CONF['user'], CONF['password'], CONF['database']) self.__cursor = self.__db.cursor()
# -*- coding: utf-8 -*- # @Time : 2/24/20 5:19 PM # @Author : Alex Hu # @Contact : [email protected] # @FileName: dbsession.py # @Software: PyCharm # @Blog : http://www.gzrobot.net/aboutme # @version : 0.1.0 from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker, scoped_session from settings.config import config_parser as conpar CONF = conpar.read_yaml_file('mysql') DB_URI = 'mysql+pymysql://{}:{}@{}:{}/{}?charset={}'.format( CONF['user'], CONF['password'], CONF['host'], CONF['port'], CONF['database'], CONF['charset']) # 创建一个engine引擎 engine = create_engine(DB_URI, echo=False, pool_size=200, pool_recycle=-1, pool_pre_ping=True) # sessionmaker生成一个session类 Session = sessionmaker(bind=engine) # 创建一个session实例 # dbSession = Session() dbSession = scoped_session(Session) # 创建一个模型基类