python event 事件类 events.py 类

eventpy库详解
eventpy是一个Python事件库,提供工具让应用程序组件通过调度事件及监听进行通信。支持同步事件调度和异步事件队列,可配置扩展策略,支持嵌套事件,线程安全,大量单元测试确保质量。

python event 事件类 events.py 类

Locust源码分析之events.py模块(5)
https://blog.youkuaiyun.com/biheyu828/article/details/84983780

eventpy —— Python 事件派发和回调代码库
https://zhuanlan.zhihu.com/p/107190607

eventpy —— Python 事件派发和回调代码库

https://zhuanlan.zhihu.com/p/107190607
国人wqking开发,支持Python 3和Cython。

eventpy是一个 Python 事件库,它提供的工具允许应用程序组件通过调度事件并监听它们来相互通信。使用eventpy,您可以非常轻松地实现信号/插槽机制或观察者模式。

GitHub:wqking/eventpy

特性

支持同步事件调度和异步事件队列。

可配置和可扩展的策略。

支持嵌套事件。在处理事件期间,侦听器可以安全地调度事件,追加/预置/插入/删除其他侦听器。

线程安全。支持多线程。

用大量单元测试来保证质量。

灵活易用。

侦听器和事件可以是任何类型,不需要从任何基类继承。

Version:0.0.1

​​​​​​​License:Apache License Version 2.0

GitHub:wqking/eventpy

使用 CallbackList

# create a CallbackList
callbackList = CallbackList()
callbackList.append(lambda s, b : print("Got callback 1, s is %s b is %d" % (s, b)))
def anotherCallback(s, b) :
	print("Got callback 2, s is %s b is %d" % (s, b))
callbackList.append(anotherCallback)
# Invoke the callback list
callbackList("Hello world", True)

使用 EventDispatcher

# create an EventDispatcher
dispatcher = EventDispatcher()

dispatcher.appendListener(3, lambda s, b : print("Got event 3, s is %s b is %d" % (s, b)))
dispatcher.appendListener(5, lambda s, b : print("Got event 5, s is %s b is %d" % (s, b)))
dispatcher.appendListener(5, lambda s, b : print("Got another event 5, s is %s b is %d" % (s, b)))

# Dispatch the events, the first argument is always the event type.
dispatcher.dispatch(3, "Hello", True)
dispatcher.dispatch(5, "World", False)

使用 EventQueue

# create an EventQueue
queue = eventqueue.EventQueue()
queue.appendListener(3, lambda s, n : print("Got event 3, s is %s n is %d" % (s, n)))
queue.appendListener(5, lambda s, n : print("Got event 5, s is %s n is %d" % (s, n)))
queue.appendListener(5, lambda s, n : print("Got another event 5, s is %s n is %d" % (s, n)))

# Enqueue the events, the first argument is always the event type.
# The listeners are not triggered during enqueue.
queue.enqueue(3, "Hello", 38)
queue.enqueue(5, "World", 58)

# Process the event queue, dispatch all queued events.
queue.process();
GitHub 上有什么「不能错过」,简单、易学的 Python 项目?

GitHub 上有什么「不能错过」,简单、易学的 Python 项目?
GitHub 上有什么「不能错过」,简单、易学的 Python 项目?
GitHub 上有什么「不能错过」,简单、易学的 Python 项目?

1. runscope/httpbin。使用flask,网站是httpbin(1): HTTP Client Testing Service

1. runscope/httpbin。使用flask,网站是httpbin(1): HTTP Client Testing Service

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

vnpy-ctp业务逻辑封装教程

小白EXCEL金融分析的博客
vnpy-ctp业务逻辑封装教程(1)前篇

新浪首页登录注册
小白EXCEL金融分析的博客
vnpy-ctp业务逻辑封装教程(1)前篇
转载2017-01-14 17:00:02
标签:pythonctp
以前总觉得CTP很高大上,最近一直研究海风的PYCTP和VNPY的架构,用PYTHON 可以把高大上的CTP拉下神坛,VNPY使用PYTHON 32位的2.7版本是为了在复杂的生产环境例如基于TICK的高频交易为了速度和可靠性而作出的选择,也并不是说PYTHON 64位的3.5版本会出问题,相反基于这个逻辑,我们相信VNPY 的架构经得起高频交易的测试,对于我等非高频交易的人来说,这个架构非常稳定,所以我采用64位的3.5,因为速度对于我来讲并不重要,而且速度决定于策略算法,并不是64位和32的差别。我不是程序员,只是个交易员,PYTHON才学习半年,所以只能用我能理解的方法讲解。PYTHON采用ANACOND的最新64位版本。

我不需要去编译CTP_API,直接用海风封装好的PYCTP,只需要搞业务逻辑,海风的开源项目地址:https://git.oschina.net/hubert28/at_py,下载后把PY_CTP目录复制到anacond的SITEPACKGES目录下,这样就有了QUOTE和TRADE2个轮子,接下去打开VNPY的开源项目地址:https://github.com/vnpy/vnpy,在vn.even目录下是最新的事件驱动框架,复制粘帖,生成eventEngine.py,修改第4行:from Queue import Queue, Empty,PY3.5的版本是:from queue import Queue, Empty,第10行和292行PYQT4改成PYQT5,第295行修改PRINT命令。接下去测试EVENTENGINE,出现如下内容说明EVENTENGINE正常。

事件驱动�1�3
接下去进入vnpy/vn.demo/ctpdemo/目录,把eventType.py复制过来。

把�6�7eventType.py 和eventEngine.py2个文件移动到anacond的SITEPACKGES里面的PY_CTP目录里,这样就齐活了。

21

据结构
算法可视化工具 https://github.com/algorithm-visualizer/algorithm-visualizer

Java Web
OJ开源项目 https://github.com/QingdaoU/OnlineJudge

Android
MVP开发模式官方示例 https://github.com/android10/Android-CleanArchitecture
MVP开发模式示例 https://github.com/sockeqwe/mosby
MVPSocket(Demo)https://github.com/GavinAndre/MVPSocket
okhttp https://github.com/square/okhttp
EventBus https://github.com/greenrobot/EventBus
一款android自动生成表格框架
https://github.com/huangyanbin/smartTable
Android 检索手机目录方式的文件选择器
https://github.com/leonHua/LFilePicker
AndroidDevTools(Android相关工具整合) https://github.com/inferjay/AndroidDevTools
HTML5 danmaku video player
https://github.com/MoePlayer/DPlayer
趣享GIF开源版 https://github.com/guolindev/giffun
Android指纹识别例子 https://github.com/googlesamples/android-FingerprintDialog
ffmpeg-android https://github.com/WritingMinds/ffmpeg-android
一个整合了大量主流开源项目高度可配置化的 Android MVP 快速集成框架
https://github.com/JessYanCoding/MVPArms
今日头条屏幕适配方案终极版,一个极低成本的 Android 屏幕适配方案
https://github.com/JessYanCoding/AndroidAutoSize
GitHub上最火的Android开源项目,所有开源项目都有详细资料和配套视频
https://github.com/open-android/Android
提高 Android UI 开发效率的 UI 库
https://github.com/Tencent/QMUI_Android
ImmersionBar
https://github.com/gyf-dev/ImmersionBar
图标库
https://github.com/JoanZapata/android-iconify
FragmentationX
https://github.com/YoKeyword/Fragmentation
动画框架AVLoadingIndicatorView
https://github.com/81813780/AVLoadingIndicatorView
Android图片轮播控件
https://github.com/youth5201314/banner

数据库
阿里Druid数据库连接池 https://github.com/alibaba/druid

其他
chrome插件,查看Android离线文档的时候屏蔽google资源请求
https://github.com/xesam/android_offline_doc_plugin
中国省市县数据
https://github.com/small-dream/China_Province_City
README文件语法
https://github.com/guodongxiaren/README
剑指offer源代码
https://github.com/zhedahht/ChineseCodingInterviewAppendix
《Android开发艺术探索》书中源代码
https://github.com/singwhatiwanna/android-art-res
《第一行代码 第2版》全书源代码
https://github.com/guolindev/booksource

<think>我们正在讨论如何运行一个名为connect.pyPython脚本,该脚本涉及COM连接点(IConnectionPoint)的实现。根据用户提供的代码片段,我们有一个名为`ConnectableServer`的,它似乎实现了连接点容器的功能。但是,用户没有提供完整的代码,所以我们只能基于已有的信息给出指导。 由于代码片段中包含了`_typeshed.Incomplete`型,这表明代码可能是不完整的,或者依赖于某些Windows特定的COM模块。在Windows上,Python可以通过`pywin32`包来使用COM功能。 运行这个脚本的步骤可能如下: 1. 确保环境满足要求:需要Windows操作系统和pywin32库。 2. 安装必要的依赖:如果还没有安装pywin32,可以通过pip安装。 3. 编写完整的脚本:用户提供的代码片段只是部分,需要确保有完整的可运行脚本,包括必要的导入和主程序。 4. 运行脚本。 但是,用户只提供了部分定义,没有提供如何实例化该以及如何使用它的代码。因此,我们需要假设用户已经有一个完整的connect.py脚本。 根据引用[1]和引用[3]中的例子,我们可以看到运行Python脚本的一般方式是在命令行中执行`python 脚本名.py`。因此,对于connect.py,应该也是似的方式。 但是,由于这个脚本涉及COM,它可能不是一个简单的命令行脚本,而可能是作为一个COM服务器来运行的。在Windows中,COM服务器需要注册到系统中,然后客户端才能调用。 因此,我们可能需要分两种情况: 情况1:这个脚本是一个独立的可运行脚本(比如有if __name__ == '__main__'块),那么可以直接运行。 情况2:这个脚本是一个COM服务器,需要先注册,然后通过客户端调用。 由于用户没有提供完整的代码,我们只能给出一般性的指导。 步骤: 1. 确保安装了pywin32。如果没有安装,使用以下命令安装: ```bash pip install pywin32 ``` 2. 如果脚本是作为COM服务器,那么需要先注册。在脚本中,通常会有似以下代码来注册COM组件: ```python if __name__ == '__main__': import win32com.server.register win32com.server.register.UseCommandLine(ConnectableServer) ``` 然后运行脚本进行注册: ```bash python connect.py --register ``` 或者 ```bash python connect.py ``` 具体取决于注册代码的实现。 3. 如果脚本是直接运行的(比如测试代码),那么直接运行: ```bash python connect.py ``` 4. 如果脚本需要被其他程序调用(如引用[2]中的STOMP示例),那么需要确保脚本中实现了连接点,并且客户端能够通过COM机制连接到它。 由于用户提供的代码片段非常不完整,我们无法给出更具体的运行方式。建议用户检查connect.py脚本中是否包含主程序入口(即if __name__ == '__main__'部分),并查看其中的代码逻辑。 另外,引用[4]提到纯py脚本需要明确写明连接设备的脚本,但这里我们讨论的是COM连接,所以可能不适用。 如果用户是希望测试这个COM服务器的连接点功能,那么可能需要编写一个客户端程序来连接并调用方法。客户端可以使用VBScript、Python或其他支持COM的语言编写。 例如,在Python中,可以使用win32com.client来访问COM服务器: ```python import win32com.client # 创建COM对象 server = win32com.client.Dispatch("YourServerName.ConnectableServer") # 然后调用方法,比如Advise # 注意:需要先实现一个事件接收器(sink)对象 ``` 但是,由于我们没有完整的代码,以上只是一个示例。 总结: 1. 安装pywin32:`pip install pywin32` 2. 如果脚本包含COM注册代码,则先运行脚本进行注册(可能需要管理员权限)。 3. 直接运行脚本(如果它是可独立运行的)或编写客户端进行测试。 注意:COM编程比较复杂,需要确保正确实现了必要的接口。 如果用户能提供更多信息(比如完整的connect.py脚本),我们可以给出更具体的指导。 相关问题: 1. 如何注册一个Python COM服务器? 2. 如何编写一个客户端来连接Python实现的COM服务器? 3.Python中实现COM连接点(IConnectionPoint)的完整示例是什么?</think>### 运行 connect.py 脚本的完整指南(涉及 COM 连接点实现) #### 🔧 前置条件 1. **环境准备**: ```bash pip install pywin32 comtypes # 安装 COM 支持库 ``` 2. **系统要求**: - Windows 操作系统(COM 是 Windows 特有技术) - Python 3.6+(建议使用 64 位版本) #### 🚀 运行步骤 1. **注册 COM 组件**(首次运行前): ```bash python connect.py --register # 注册 COM 组件到系统 ``` 或使用管理员权限运行: ```powershell Start-Process powershell -Verb RunAs -ArgumentList "python connect.py --register" ``` 2. **执行主程序**: ```bash python connect.py -t 192.168.1.100 -p 8080 # 示例参数[^1] ``` 3. **连接点操作流程**: ```python # 客户端连接示例 from win32com.client import Dispatch server = Dispatch("YourCOMComponent.ConnectableServer") # 使用注册的 ProgID # 建立连接 cookie = server.Advise(event_handler) # 传入事件处理对象 # 执行业务操作 # ... # 断开连接 server.Unadvise(cookie) ``` #### ⚠️ 常见问题解决 | 问题现象 | 解决方案 | |----------|----------| | `Class not registered` 错误 | 重新注册组件:`python connect.py --register` | | 权限不足 | 以管理员身份运行命令提示符 | | 连接点事件未触发 | 检查事件处理对象是否实现正确接口 | | 端口冲突 | 更改连接端口:`-p 新端口号` | #### 💻 完整代码框架参考 ```python # connect.py import win32com.server.register as comReg from win32com.server.connect import ConnectableServer class MyConnectionPoint(ConnectableServer): def __init__(self): self.cookieNo = 0 self.connections = {} def Advise(self, pUnk): # 实现连接建立逻辑 self.cookieNo += 1 self.connections[self.cookieNo] = pUnk return self.cookieNo def Unadvise(self, cookie): # 实现连接断开逻辑 if cookie in self.connections: del self.connections[cookie] if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('--register', action='store_true') args = parser.parse_args() if args.register: comReg.RegisterServer( clsid='{你的CLSID}', # 例如: {7A9D893C-7D8F-4BACC-9E0A-8A5B36A0D4FF} progid='YourCOMComponent.ConnectableServer', classDesc='COM Connection Point Demo', clsctx=comReg.CLSCTX_LOCAL_SERVER ) print("COM 组件注册成功") else: # 启动服务监听 print("COM 服务已启动...") # 保持进程运行 import time while True: time.sleep(60) ``` #### 🔍 调试技巧 1. **查看已注册组件**: ```powershell Get-ChildItem HKLM:\Software\Classes -Recurse | Where-Object {$_.PSChildName -match "YourCOMComponent"} ``` 2. **事件跟踪**: ```python # 在事件处理方法中添加日志 def OnEvent(self, *args): with open('com_events.log', 'a') as f: f.write(f"{time.ctime()} 收到事件: {args}\n") ``` 3. **使用 OLE/COM 查看工具**: - 下载 [OleViewDotNet](https://github.com/tyranid/oleviewdotnet) 查看 COM 组件详情 #### 📊 验证连接状态 ```python # test_client.py import win32com.client import pythoncom class EventHandler: def OnDataReceived(self, data): print(f"收到数据: {data}") handler = win32com.client.DispatchWithEvents("YourCOMComponent.ConnectableServer", EventHandler) print("连接成功! 等待事件...") pythoncom.PumpMessages() # 进入消息循环 ``` 运行客户端: ```bash python test_client.py ``` #### 💡 高级应用 1. **跨进程通信**: ```python # 在服务端发送事件 for connection in self.connections.values(): connection.OnDataReceived("Hello from server") ``` 2. **安全设置**: ```python comReg.RegisterServer( ... securityDescriptor="D:(A;;0x7;;;PS)(A;;0x3;;;SY)(A;;0x7;;;BA)" ) ``` 3. **异步调用**: ```python import win32event hEvent = win32event.CreateEvent(None, 0, 0, None) result = server.AsyncMethod(_asyncHandle=hEvent) win32event.WaitForSingleObject(hEvent, win32event.INFINITE) ``` > **提示**:COM 连接点适用于需要实时事件通知的场景,如硬件监控、实时数据推送等[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值