def __init__(self, *, port: str, baudrate: int, path: str = 'db', bs: int = 32, to: int = 5, http: int = 8080): super().__init__() pid = os.getpid() logger.info('*** NMEA Recorder startup (%d)' % pid) self.process: Dict[str, int] = {} self.process['main'] = pid self.ppp: Dict[str, Patrol] = {} self.ppp['main'] = Patrol(pid=pid) self.ready: bool = True self.g = GPS() self.ws = WebsocketServer(debug=True) self.qp = Queue() self.dbsession = DBSession(path=pathlib.Path(path), buffersize=bs, timeout=to) self.dbsession.start() self.process[self.dbsession.name] = self.dbsession.pid self.ppp[self.dbsession.name] = Patrol(pid=self.dbsession.pid) self.receiver = Receiver(port=port, baudrate=baudrate, qp=self.qp) self.receiver.start() self.process[self.receiver.name] = self.receiver.pid self.ppp[self.receiver.name] = Patrol(pid=self.receiver.pid) self.mc = fromUDP(quePoint=self.qp, mcip='239.192.0.1', mcport=60001) self.mc.start() self.process[self.mc.name] = self.mc.pid self.ppp[self.mc.name] = Patrol(pid=self.mc.pid) self.main = Thread(target=self.collector, name='MainLoop', daemon=True) self.main.start() self.p = Thread(target=self.patrol, name='Patrol', daemon=True) self.p.start() for k, v in self.ppp.items(): v.start() self.add_route('/ws', self.ws.wsserver, websocket=True) self.add_route('/', self.top) self.add_route('/main.js', self.mainJS) self.add_route('/classes.js', self.classes) self.add_event_handler('shutdown', self.cleanup) # notice! self.run(address='0.0.0.0', port=http)
async def get_db(): db = DBSession() try: ''' 不要尝试在yield后面抛出HTTPException,不会起作用 yield之后的退出,是在异常处理器之后被执行的 ''' yield db finally: db.close()
def __init__(self): super().__init__() logger.info('### startup') self.running = True self.lack: Dict[str, Stock] = {} self.holdSecs = (60 * 2) self.locker = Lock() self.collector = Collector() self.collector.start() self.dbsession = DBSession(path='NMEA', buffersize=1000) self.dbsession.start() self.ws = WebsocketServer(debug=True) self.add_route('/ws', self.ws.wsserver, websocket=True) self.counter = 0 self.ticker = Event() signal.signal(signal.SIGALRM, self.clocker) signal.setitimer(signal.ITIMER_REAL, 1, 1) self.cycle = Thread(target=self.routineWorker, daemon=True) self.cycle.start() self.mainLoop = Thread(target=self.loop, daemon=True) self.mainLoop.start() # logger.debug('$$$ collector = %d' % self.collector.pid) # logger.debug('$$$ dbsession = %d' % self.dbsession.pid) self.run(address='0.0.0.0', port=8080) self.running = False # self.collector.join() self.dbsession.join() logger.info('shutdown')
def __init__(self): self.db = DBSession()
#!/usr/bin/env python # -*- coding: utf-8 -*- from dbsession import DBSession dbSession = DBSession()