修改Python的pyxmpp2中的主循环使其提高性能
作者:C Wong 发布时间:2022-07-22 15:26:53
标签:Python
引子
之前clubot使用的pyxmpp2的默认mainloop也就是一个poll的主循环,但是clubot上线后资源占用非常厉害,使用strace跟踪发现clubot在不停的poll,查看pyxmpp2代码发现pyxmpp2的poll在使用超时阻塞时使用最小超时时间,而最小超时时间一直是0,所以会变成一个没有超时的非阻塞poll很浪费资源,不打算更改库代码,所以自己仿照poll的mainloop写了一个更加高效的epoll的mainloop
实现
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# Author : cold
# E-mail : wh_linux@126.com
# Date : 13/01/06 10:41:31
# Desc : Clubot epoll mainloop
#
from __future__ import absolute_import, division
import select
from pyxmpp2.mainloop.interfaces import HandlerReady, PrepareAgain
from pyxmpp2.mainloop.base import MainLoopBase
from plugin.util import get_logger
class EpollMainLoop(MainLoopBase):
""" Main event loop based on the epoll() syscall on Linux system """
READ_ONLY = (select.EPOLLIN | select.EPOLLPRI | select.EPOLLHUP |
select.EPOLLERR |select.EPOLLET)
READ_WRITE = READ_ONLY | select.EPOLLOUT
def __init__(self, settings = None, handlers= None):
self.epoll = select.epoll()
self._handlers = {}
self._unprepared_handlers = {}
self._timeout = None
self._exists_fd = {}
self.logger = get_logger()
MainLoopBase.__init__(self, settings, handlers)
return
def _add_io_handler(self, handler):
self._unprepared_handlers[handler] = None
self._configure_io_handler(handler)
def _configure_io_handler(self, handler):
if self.check_events():
return
if handler in self._unprepared_handlers:
old_fileno = self._unprepared_handlers[handler]
prepared = self._prepare_io_handler(handler)
else:
old_fileno = None
prepared = True
fileno = handler.fileno()
if old_fileno is not None and fileno != old_fileno:
del self._handlers[old_fileno]
self._exists.pop(old_fileno, None)
self.epoll.unregister(old_fileno)
if not prepared:
self._unprepared_handlers[handler] = fileno
if not fileno:
return
self._handlers[fileno] = handler
events = 0
if handler.is_readable():
events |= self.READ_ONLY
if handler.is_writable():
events |= self.READ_WRITE
if events:
if fileno in self._exists_fd:
self.epoll.modify(fileno, events)
else:
self._exists_fd.update({fileno:1})
self.epoll.register(fileno, events)
def _prepare_io_handler(self, handler):
ret = handler.prepare()
if isinstance(ret, HandlerReady):
del self._unprepared_handlers[handler]
prepared = True
elif isinstance(ret, PrepareAgain):
if ret.timeout is not None:
if self._timeout is not None:
self._timeout = min(self._timeout, ret.timeout)
else:
self._timeout = ret.timeout
prepared = False
else:
raise TypeError("Unexpected result from prepare()")
return prepared
def _remove_io_handler(self, handler):
if handler in self._unprepared_handlers:
old_fileno = self._unprepared_handlers[handler]
del self._unprepared_handlers[handler]
else:
old_fileno = handler.fileno()
if old_fileno is not None:
try:
del self._handlers[old_fileno]
self._exists.pop(old_fileno, None)
self.epoll.unregister(old_fileno)
except KeyError:
pass
def loop_iteration(self, timeout = 60):
next_timeout, sources_handled = self._call_timeout_handlers()
if self.check_events():
return
if self._quit:
return sources_handled
for handler in list(self._unprepared_handlers):
self._configure_io_handler(handler)
if self._timeout is not None:
timeout = min(timeout, self._timeout)
if next_timeout is not None:
timeout = min(next_timeout, timeout)
if timeout == 0:
timeout += 1 # 带有超时的非阻塞,解约资源
events = self.epoll.poll(timeout)
for fd, flag in events:
if flag & (select.EPOLLIN | select.EPOLLPRI | select.EPOLLET):
self._handlers[fd].handle_read()
if flag & (select.EPOLLOUT|select.EPOLLET):
self._handlers[fd].handle_write()
if flag & (select.EPOLLERR | select.EPOLLET):
self._handlers[fd].handle_err()
if flag & (select.EPOLLHUP | select.EPOLLET):
self._handlers[fd].handle_hup()
#if flag & select.EPOLLNVAL:
#self._handlers[fd].handle_nval()
sources_handled += 1
self._configure_io_handler(self._handlers[fd])
return sources_handled
使用
如何使用新的mainloop?只需在实例化Client时传入
mainloop = EpollMainLoop(settings)
client = Client(my_jid, [self, version_provider], settings, mainloop)
这样就会使用epoll作为mainloop
注意
epoll仅仅在Linux下支持
0
投稿
猜你喜欢
- <?php ////$strimgsrc = file_get_contents("http://127.0.0.1/530
- 在平时的工作中,我们的目录有很多的视频文件,如果你没有一个好的视频分类习惯,在找视频素材的时候会很费时,通过对视频的分辨路进行分类可以在需要
- 废话不多说了,上代码吧:import threadingimport requestsimport timeimport osclass M
- 在学习OpenCV或者其他关于Python技术的时候,我们通常需要准备不同的Python环境,我选择了Anaconda作为我的Python环
- Python提取html中文本到txt正则去标签方式# -*- coding: utf-8 -*-import redef html_tag
- numpy.amin()和numpy.amax()numpy.amin()用于计算数组中元素沿着指定轴的最小值。numpy.amax()用于
- 然后给脚本文件运行权限,方法(1)chmod +x ./*.py方法(2)chmod 755 ./*.py (777也无所谓啦)这个命令不去
- SQL Server 2000安装问题集锦1、先把SQL Server卸载(卸载不掉也没有关系,继续下面的操作)2、把Microsoft S
- 前言我们知道在这个互联网时代,评论已经在我们的生活到处可见,评论区里面的信息是一个非常有趣和有争议的地方。我们今天,就来获取某技术平台的评论
- 使用Python内置函数:bin()、oct()、int()、hex()可实现进制转换。 先看Python官方文档中对这几个内置函数的描述:
- 数据类型是所有开发语言的基础,JavaScript虽然是一个弱类型的脚本语言,但是在数据类型上也有很多讲究的,看了淘宝UED玉伯的一篇文章,
- 介绍Go使用goroutines来处理connection的读写事件,不会阻塞:c, err := srv.newConn(rw) &nbs
- 之前我们曾经分享过:Python获取某一日期是“星期几”的6种方法!实际上,在我们使用Python处
- 有两种做法:os.walk()、pathlib库,个人感觉pathlib库的path.glob用来匹配文件比较简单。下面是第二种做法的实例(
- 刚才显示数据的时候遇到一个日期里面带T的问题,就是天数跟小时数之间出现了一个T。 表字段里面也没有这个T,后来查询度娘,是因为json处理的
- 实例化对象名._类名__私有属性名 class Flylove:price = 123 def __init__(self):s
- WINDOWS 1、MySQL是有一些环境变量可以设置, 可输入:show variables;可查看当前的环境变量设置; 2、这些变量当中
- 本文实例讲述了CodeIgniter自定义控制器MY_Controller用法。分享给大家供大家参考,具体如下:Codeigniter所有的
- CAS算法(compare and swap)CAS算法是一种有名的无锁算法。无锁编程,即不使用锁的情况下实现多线程之间的变量同步,也就是在
- 循环可以用来重复执行某条语句,直到某个条件得到满足或遍历所有元素。1 for循环是for循环,可以把集合数据类型list、tuple、dic