惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Project Zero
Project Zero
T
Tailwind CSS Blog
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
L
LINUX DO - 热门话题
Spread Privacy
Spread Privacy
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Heimdal Security Blog
博客园 - 【当耐特】
W
WeLiveSecurity
J
Java Code Geeks
Latest news
Latest news
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Troy Hunt's Blog
博客园 - Franky
月光博客
月光博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
PCI Perspectives
PCI Perspectives
博客园_首页
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
P
Palo Alto Networks Blog
I
InfoQ
Security Latest
Security Latest
Hacker News: Ask HN
Hacker News: Ask HN
Microsoft Azure Blog
Microsoft Azure Blog
M
MIT News - Artificial intelligence
Help Net Security
Help Net Security
F
Full Disclosure
Cyberwarzone
Cyberwarzone
D
DataBreaches.Net
The Cloudflare Blog
S
Securelist
美团技术团队
C
Cybersecurity and Infrastructure Security Agency CISA
AI
AI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events

博客园 - 凌度

unity加密方案 [C#]AES加解密 vs20109自动补全unity 机器学习 爬取tieba的APP点赞等数据 爬取douyin 数据仓库和数据湖 通过mitmproxy爬取APP的数据 linux下检查网络连通情况 linux下使用clamav排查病毒 GIT统计代码行数 [linux]测硬盘读写速度、内存读写速度 QPS/TPS的预估 [python]pypy优化python性能 [linux]查看进程占用内存 [linux]杀死同一个应用的所有进程 [LINUX] 快速回收连接 jdbc批量写入 Python执行时间的计算方法
tornado多进程模式不同进程写不同日志
凌度 · 2019-09-26 · via 博客园 - 凌度
#coding: utf-8
'''
Author:
Time: 
Target: 
'''

import logging
import logging.handlers
import os
import json
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.options import define, options
import time
import gc



define("port", default=8000, help="Please send email to me", type=int)




def init_log(log_filename, pattern='test'):
    '''
    记录log日志
    :param log_filename:日志记录名
    :param pattern: 类型
    :return:
    '''
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    logger = logging.getLogger()
    if pattern == 'production':
        fh = logging.handlers.TimedRotatingFileHandler(
            filename=log_filename, when='midnight')
        fh.suffix = '%Y%m%d.log'
        logger.setLevel(logging.INFO)
    elif pattern == 'rotating_test':
        fh = logging.handlers.TimedRotatingFileHandler(
            filename=log_filename, when='M')
        fh.suffix = '%Y%m%d-%H%M.log'
        logger.setLevel(logging.DEBUG)
    elif pattern == 'test':
        fh = logging.FileHandler(filename=log_filename, mode='w')
        logger.setLevel(logging.DEBUG)
    else:
        #print('No Legal Pattern')
        raise TypeError
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    return logger




if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        handlers = [
            (r"/update", update),
            (r"/Cleantext",clean)
        ]
    )
    '''
    # 第一种多进程启动模式
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    http_server.start(4)
    #http_server.start()
    tornado.ioloop.IOLoop.instance().start()

    '''
    
    # 第二种多进程启动模式
    sockets = tornado.netutil.bind_sockets(options.port)
    task_id = tornado.process.fork_processes(4)
    #取到的task_id 是0~3。os.getpid()取子进程ID,os.getppid()取父进程ID
    if task_id is None:
        task_id = str(os.getpid()) + "_" + str(os.getppid())
    
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.add_sockets(sockets)

    #global LOGGER    
    LOG_FOLDER = './server_%s_log'%(options.port)
    if not os.path.exists(LOG_FOLDER):
        os.makedirs(LOG_FOLDER)
    LOG_PATH = os.path.join(LOG_FOLDER, 'ServerLog')
    # print(LOG_PATH)
    # 每个子进程自己创建一个日志文件
    LOGGER = init_log(LOG_PATH + str(task_id) , pattern='production')

    tornado.ioloop.IOLoop.instance().start()   
class update(tornado.web.RequestHandler):
    
    def post(self):

        result = {
            'status': 'success'
        }

    post_data = self.request.body
        time1=time.time()
        post_data=post_data.decode('utf-8')
        try:
            post_data=json.loads(post_data)
        except Exception as e:
            post_data=post_data.replace('\n','').replace('\r','')
            post_data = json.loads(post_data)

    #print(post_data)
        channel=post_data['channel']
        #print(channel)
        sentence=post_data['content']
        

        self.write(json.dumps(post_data))
        self.add_header('Content-Type', 'application/json')

    time2=time.time()
        LOGGER.info('INFO 200 POST/clean  %2.5f ms'%(1000*(time2-time1)))