iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python之selectors
  • 451
分享到

python之selectors

pythonselectors 2023-01-30 22:01:02 451人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

selectors是select模块的包装器,ptython文档建议大部分情况使用selectors而不是直接使用selectors 样例代码如下 # -*- coding: utf-8 -*- __author__ = 'fc' i

selectors是select模块的包装器,ptython文档建议大部分情况使用selectors而不是直接使用selectors

样例代码如下

# -*- coding: utf-8 -*-
__author__ = 'fc'

import selectors
import Socket
import time

def accept(sock:socket.socket, mask):
    conn, addr = sock.accept()
    print('client', addr, 'connected')
    conn.setblocking(False)
    sel.reGISter(conn, selectors.EVENT_READ, oper)

def oper(sock:socket.socket, mask):
    if selectors.EVENT_READ & mask:
        print('ready to read')
        data = sock.recv(1024)
        if not data:
            print('client', sock.getpeername(), 'disconnnected')
            sel.unregister(sock)
            sock.close()
        else:
            print('received: ', data)
            sel.modify(sock, selectors.EVENT_WRITE, oper)
    elif selectors.EVENT_WRITE & mask:
        print('ready to write')
        msg = b'hello, friend'
        sock.send(msg)
        print('send: ', msg)
        sel.modify(sock, selectors.EVENT_READ, oper)


sel = selectors.DefaultSelector()
print(sel)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.bind(('', 60001))
sock.listen(5)
sock.setblocking(False)
sel.register(sock, selectors.EVENT_READ, accept)

while True:
    events = sel.select(10)
    if not events:
        print('timeout', time.strftime('%Y-%m-%d %H:%M:%S'))
    for key, mask in events:
        callback = key.data
        callback(key.fileobj, mask)

 

--结束END--

本文标题: python之selectors

本文链接: https://www.lsjlt.com/news/179848.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python之selectors
    selectors是select模块的包装器,ptython文档建议大部分情况使用selectors而不是直接使用selectors 样例代码如下 # -*- coding: utf-8 -*- __author__ = 'fc' i...
    99+
    2023-01-30
    python selectors
  • python之selectors模块
      selectors模块是在python3.4版本中引进的,它封装了IO多路复用中的select和epoll,能够更快,更方便的实现多并发效果。  官方文档见:https://docs.python.org/3/library/...
    99+
    2023-01-31
    模块 python selectors
  • Python selectors
    #!/usr/bin/env python # -*- coding:utf-8 -*- # author: Changhua Gong import selectors import socket sel = selecto...
    99+
    2023-01-31
    Python selectors
  • selectors怎么在python中使用
    本篇文章给大家分享的是有关selectors怎么在python中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。python的数据类型有哪些python的数据类型:1. 数字...
    99+
    2023-06-14
  • 解决:Some selectors are not allowed in component wxss, including tag name selectors, ID selectors
    在微信开发工具中运行文档中的代码,出现如下错误:  [渲染层错误] Some selectors are not allowed in component wxss, including tag name selectors, ID s...
    99+
    2023-09-07
    前端 javascript 开发语言 小程序
  • css selectors的含义是什么
    这篇“css selectors的含义是什么”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“css selectors的含义是...
    99+
    2023-07-04
  • Python之——python-nmap
    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/78995578 一、python-nmap安装 # yum -y install nmap #安装nmap工具 # ...
    99+
    2023-01-31
    Python python nmap
  • Python全栈之路系列之Python
    The Python interpreter has a number of functions and types built into it that are always available. They are listed her...
    99+
    2023-01-31
    之路 系列之 Python
  • python3--IO模型,阻塞,非阻塞,多路复用,异步,selectors模块
    协程回顾协程 实际上是一个线程执行了多个任务,遇到IO就切换示例:import time import gevent def func():     print('...
    99+
    2023-01-30
    多路 复用 模块
  • python之numpy.tile()
    格式:tile(A,reps) * A:array_like * 输入的array * reps:array_like * A沿各个维度重复的次数举例:A=[1,2] 1. tile(A,2) 结果:[1,2,1,2] 2. tile(A,...
    99+
    2023-01-31
    python numpy tile
  • python之VSCode
    1、安装python3.5(3.6版本亲测无法实现命令补全功能)2、安装VSCode(我的版本:VSCode-win32-1.7.2)3、安装Python插件安装Python插件能实现语法提示的一些功能,建议还是安装一下。打开VScode,...
    99+
    2023-01-31
    python VSCode
  • Python之MySQL
    创建连接标签(空格分隔): Python学习 mysql数据库存储数据的方式与excel类似,都是以表格的形式来存储数据。excel一般用一张表来存储少量的数据,数据库可以用多个表来存储大量的数据。 用其他方式存储数据,如果数据量少,读...
    99+
    2023-01-31
    Python MySQL
  • python之day1
      初学python会有学python2还是python3的困惑,因为现在公司的实际环境下大部分还是用2.7编写的代码,python3.X又向下不兼容2.X,我认为作为初学者更应该从python3着手:python3和python2区别不是...
    99+
    2023-01-31
    python
  • python之socket
    python之socket一、初识socket        socket 是网络连接端点,每个socket都被绑定到一个特定的IP地址和端口。IP地址是一个由4个数组成的序列,这4个数均是范围 0~255中的值(例如,220,176,36...
    99+
    2023-01-31
    python socket
  • Python 之 matplotlib
    代码: import matplotlib.pyplot as plt import numpy as np from matplotlib import animation fig, ax = plt.subplots() x = ...
    99+
    2023-01-31
    Python matplotlib
  • Python之Pool
    #!/usr/bin/env pythonfrom multiprocessing import Poolimport timedef sayHi(a):        time.sleep(5)        return a**aif ...
    99+
    2023-01-31
    Python Pool
  • python之路
    一切资源皆可用,只为学到东西!!!http://www.cnblogs.com/wupeiqi/articles/4938499.htmlhttp://www.cnblogs.com/wupeiqi/tag/Python/         ...
    99+
    2023-01-31
    之路 python
  • Python之RabbitMQ
    RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件。RabbitMQ服务器是用Erlang语言编写的,它可以为你的应用提供一个通用的消息发送和接收平台,并且保证消息在传输过程中的安全,RabbitMQ官网,RabbitM...
    99+
    2023-01-31
    Python RabbitMQ
  • Python 之NumPy
    NumPy的主要对象是同质的多维数组。它是一个有明确索引的相同类型的元素组成的表。在NumPy中维度称之为轴,轴数称之为列。举个例子:例一:[ 1, 2, 1 ]这是一个一维数组,因为它只有一个轴,这个轴的长度是3.列二:[[ 1., 0....
    99+
    2023-01-31
    Python NumPy
  • python之禅
    Beautiful is better than ugly.Explicit(明确的,清楚的) is better than implicit.    Simple is better than complex.Complex(复杂的;合成...
    99+
    2023-01-30
    python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作