跨平台python异步回调机制实现和使用方法
时间:2021-12-18 23:33:26
1 将下面代码拷贝到一个文件,命名为asyncore.py
import socket
import select
import sys
def ds_asyncore(addr,callback,timeout=5):
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(addr)
r,w,e = select.select([s],[],[],timeout)
if r:
respose_data=s.recv(1024)
callback(respose_data)
s.close()
return 0
else:
s.close()
return 1
2 编写自己的代码
1> 导入asyncore
2> 定义回调函数callback,callback需要一个参数,代表请求返回数据
3> 直接调用asyncore.ds_asyncore(('127.0.0.1', 33333),callback,timeout=5),其中第一个参数是一个(ip,port)元组,第二个是回调函数,第三个是超时时间。
import asyncore
if __name__=="__main__":
def callback(respose_data):
print respose_data
asyncore.ds_asyncore(('127.0.0.1', 33333),callback,timeout=5)
注:此代码可在windows,linux上运行
标签:python,异步回调,跨平台
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
msxml3.dll 错误 ‘800c0005’解决方案
2009-10-05 18:36:00
PHP PDOStatement::fetchColumn讲解
2023-06-06 09:17:20
Tensorflow 多线程设置方式
2021-09-29 21:53:50
python接口自动化使用requests库发送http请求
2023-08-02 02:04:37
![](https://img.aspxhome.com/file/2023/0/61490_0s.png)
浅谈python多线程和队列管理shell程序
2023-05-18 05:58:25
如何实现html表格里隔行换色
2007-10-19 13:42:00
python三大器之迭代器、生成器、装饰器
2022-04-14 13:44:20
![](https://img.aspxhome.com/file/2023/3/102923_0s.jpg)
pytorch 输出中间层特征的实例
2022-06-08 07:36:39
Ajax标签导航效果
2013-07-17 02:02:16
python 爬取腾讯视频评论的实现步骤
2021-06-19 03:57:58
![](https://img.aspxhome.com/file/2023/3/90343_0s.png)
python使用turtle库绘制时钟
2022-09-13 07:06:51
![](https://img.aspxhome.com/file/2023/6/72856_0s.jpg)
在Python中使用M2Crypto模块实现AES加密的教程
2022-09-29 17:43:59
Python全景系列之模块与包全面解读
2022-12-09 19:26:48
python中用Scrapy实现定时爬虫的实例讲解
2023-07-26 05:47:01
使用Python抓取模板之家的CSS模板
2022-04-09 18:35:52
![](https://img.aspxhome.com/file/2023/7/79457_0s.jpg)
numpy数组叠加的实现示例
2021-09-29 11:59:18
Python中线程threading.Thread的使用详解
2023-07-22 13:25:48
Python解决爬虫程序卡死问题
2021-04-02 15:34:32
![](https://img.aspxhome.com/file/2023/2/78582_0s.webp)
bootstrapTable+ajax加载数据 refresh更新数据
2023-10-18 13:21:13
CentOS 6.5中安装Python 3.6.2的方法步骤
2022-10-11 09:13:43