Gij*_*aar 4 c# python dll multithreading python.net
我正在使用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消息处理循环.
可以使用在Windows事件循环中插入执行Form.Invoke.运行非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()
Run Code Online (Sandbox Code Playgroud)
一些相关链接:
| 归档时间: |
|
| 查看次数: |
755 次 |
| 最近记录: |