我正在使用Python .NET围绕黑盒.NET DLL构建Python API. DLL只进行网络操作. DLL要求我运行一个窗口消息泵循环,否则网络操作会在一段时间后卡住.我在主线程中使用System.Windows.Forms.Application.Run()运行Windows...

我正在使用Python .NET围绕黑盒.NET DLL构建Python API. DLL只进行网络操作. DLL要求我运行一个窗口消息泵循环,否则网络操作会在一段时间后卡住.我在主线程中使用System.Windows.Forms.Application.Run()运行Windows消息循环.这适用于仅接收数据.当我开始从其他Python线程调用DLL时,我的程序开始表现得很奇怪.我认为它与线程有关,因为问题非常不规律 – 网络事件消失或进入很晚.据我所知,Python和C#是线程安全的,但也许是因为多层包装出错了.
所以我有几个问题:
>从多个Python theads中调用DLL是不是一个坏主意?或者这取决于DLL内部?我认为从DLL的角度来看,由于GIL,Python线程被视为一个单独的代理.
>使用此DLL的每个Python线程都需要消息泵吗?
>将所有DLL交互保存在一个线程中可能是个好主意.我很难完成这个,因为我控制主线程中的消息泵循环.我天真的方法是将我的工作线程中生成的新传出网络消息放在Python队列上,在主线程中创建一个自定义消息泵循环来执行windows事件处理,但也监视我的队列,如果有消息则会执行调用DLL.但是对于这么简单的任务来说,这一切都非常笨重而且很多工作.这是正确的方法吗?
>是否有另一种方法将函数放在主窗口事件循环中,监视先前描述的队列并采取行动?或者我应该深入研究.NET细节并开始使用.NET事件或调度程序?
解决方法:
我找到了大部分问题的答案.
>我认为我们不能假设DLL是线程安全的,所以最好将所有与DLL的交互隔离到一个线程.
>看起来Windows消息系统是每个线程而不是每个进程,所以是的,每个使用Windows消息系统的线程都需要有一个Windows消息处理循环.
>可以使用Form.Invoke在Windows事件循环中插入执行.运行非ui主循环,您可以使用Dispatcher.CurrentDispatcher获取调度程序,该调度程序可用于“调用”函数.然后在创建调度程序的线程上执行调用的函数.需要委托被调用的函数,这是一个C#特定的东西来传递对函数的引用.
最后我为非ui主循环做了类似的事情:
import clr
import threading
# we need to get access to the threading assembly
clr.AddReference("c:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\v3.0\\WindowsBase.dll")
import System
from System.Windows.Threading import Dispatcher
from System.Windows.Forms import Application
# now get the dispatcher for the current thread
dispatcher = Dispatcher.CurrentDispatcher
def display():
print(threading.current_thread())
# now make this a deligate
deligate = System.Action(display)
def other_thread(dispatcher):
# now you can use the dispatcher from the main thread to
# to schedule a run from an other thread
dispatcher.Invoke(deligate)
thread = threading.Thread(target=other_thread, args=(dispatcher,))
thread.start()
Application.Run()
一些相关链接:
> more info about the Dispatcher
> more about the Action delegate
本文标题为:c# – Python .NET,多线程和windows事件循环


基础教程推荐
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C# 解析XML和反序列化的示例 2023-04-14
- C#中的Linq to JSON操作详解 2023-06-08
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#中 Json 序列化去掉null值的方法 2022-11-18
- Unity shader实现高斯模糊效果 2023-01-16
- 实例详解C#实现http不同方法的请求 2022-12-26