1 Star 0 Fork 13

william-ye/qd

forked from qd-today/qd 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
run.py 3.12 KB
一键复制 编辑 原始数据 按行查看 历史
a76yyyy 提交于 2023-06-04 23:44 . chore(qd): fix qd.py failure
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2016 Binux <roy@binux.me>
import asyncio
import json
import logging
import os
import platform
import sys
import tornado.log
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop, PeriodicCallback
import config
from db import DB, db_converter
from db.basedb import engine
from libs.log import Log
from web.app import Application
from worker import BatchWorker, QueueWorker
def start_server():
if sys.getdefaultencoding() != 'utf-8':
import importlib
importlib.reload(sys)
# init logging
logger = Log().getlogger()
logger_QD = Log('QD.Run').getlogger()
if config.debug:
channel = logging.StreamHandler(sys.stderr)
channel.setFormatter(tornado.log.LogFormatter())
channel.setLevel(logging.WARNING)
logger_QD.addHandler(channel)
if not config.accesslog:
tornado.log.access_log.disabled = True
else:
tornado.log.access_log = Log('tornado.access').getlogger()
# tornado.log.app_log = Log('tornado.application').getlogger()
if len(sys.argv) > 2 and sys.argv[1] == '-p' and sys.argv[2].isdigit():
port = int(sys.argv[2])
else:
port = config.port
if platform.system() == 'Windows':
config.multiprocess = False
if config.multiprocess and config.autoreload:
config.autoreload = False
try:
database = DB()
converter = db_converter.DBconverter(database)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
run = asyncio.ensure_future(converter.ConvertNewType(database) , loop=loop)
loop.run_until_complete(run)
default_version = json.load(open(os.path.join(os.path.dirname(__file__), 'version.json'),'r', encoding='utf-8'))['version']
App= Application(database, default_version)
http_server = HTTPServer(App, xheaders=True)
http_server.bind(port, config.bind)
if config.multiprocess:
http_server.start(num_processes=0)
else:
http_server.start()
io_loop = IOLoop.instance()
try:
if config.worker_method.upper() == 'QUEUE':
worker = QueueWorker(database)
io_loop.add_callback(worker)
elif config.worker_method.upper() == 'BATCH':
worker = BatchWorker(database)
PeriodicCallback(worker, config.check_task_loop).start()
else:
raise Exception('worker_method must be Queue or Batch, please check config!')
except Exception as e:
logger.exception('worker start error!')
raise KeyboardInterrupt()
logger_QD.info("Http Server started on %s:%s", config.bind, port)
io_loop.start()
except KeyboardInterrupt :
logger_QD.info("Http Server is being manually interrupted... ")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
run = asyncio.ensure_future(engine.dispose() , loop=loop)
loop.run_until_complete(run)
logger_QD.info("Http Server is ended. ")
if __name__ == "__main__":
start_server()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/william-ye/qd.git
git@gitee.com:william-ye/qd.git
william-ye
qd
qd
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385