python关于线程池和进程池的乱用
发布:2019-04-28 07:23:52
关于线程池和进程池的破应用
撸了这么一个奇奇怪怪的东西
恩。。至少知道咋用了
需要cpu消耗的走慢队列 进 进程池
需要io消耗的走快队列 走 线程池
丢到asyncio的框架里用来处理非异步库应该会可以的
emmmmm
import logging
from concurrent.futures import ThreadPoolExecutor
import threading
from queue import Queue
from utils import route
class MsgProtcl:
def __init__(self,id,msg):
self.id = id
self.msg = msg
def __repr__(self):
return f"id:{self.id}msg:{self.msg}"
class MsgEventPool():
def __init__(self):
self.__msgQueue = Queue()
self.__pool = ThreadPoolExecutor(max_workers=3, thread_name_prefix='fish_event')
self.__isRun = False
self.__router = {}
def addMsg(self,msg):
self.__msgQueue.put(msg)
def start(self):
if not self.__isRun:
self.__isRun = True
t1= threading.Thread(target=self.__getmsg)
t1.setName("msgThread")
t1.setDaemon(True)#设置为后台线程,这里默认是False,设置为True之后则主线程不用等待子线程
t1.start()
def __poolrun(self,msgObj:MsgProtcl):
logging.debug(threading.current_thread())
"""
在这里插入路由req和res
"""
route.init(msgObj.id,msgObj.msg)
def __getmsg(self):
logging.debug(threading.current_thread())
while True:
self.__pool.submit(self.__poolrun,self.__msgQueue.get())
msgEvent = MsgEventPool()
callback
def parse(obj):
res=obj.result()
t.submit(get,url).add_done_callback(parse)
查询时间:3.339ms
渲染时间:5.289ms