c# - Python .NET、多线程和 Windows 事件循环

标签 c# python multithreading dll python.net

我正在使用 Python .NET 围绕黑盒 .NET DLL 构建 Python API。 DLL 仅执行网络操作。 DLL 要求我运行 Windows 消息泵循环,否则网络操作会在一段时间后卡住。我在主线程中使用 System.Windows.Forms.Application.Run() 运行 Windows 消息循环。这仅适用于接收数据。尽管当我开始从另一个 Python 线程调用 DLL 时,我的程序开始表现得很奇怪。我认为它与线程有关,因为问题的发生非常不规律——网络事件消失或出现得很晚。据我所知,Python 和 C# 是线程安全的,但可能是因为多层包装出了问题。

所以我有几个问题:

  • 从多个 Python thead 调用 DLL 是不是一个坏主意?或者这是否取决于 DLL 内部结构?由于 GIL,我认为从 DLL 的角度来看,Python 线程被视为一个单独的代理。

  • 是否每个使用此 DLL 的 Python 线程都需要一个消息泵?

  • 将所有 DLL 交互保留在一个线程中可能是个好主意。我很难做到这一点,因为我把控制权交给了主线程中的消息泵循环。我天真的方法是将在我的工作线程中生成的新传出网络消息放在 Python 队列中,在主线程中创建一个自定义消息泵循环,该循环执行 Windows 事件处理,但也监视我的队列,如果有消息将执行调用 DLL。但是对于这样一个简单的任务,这一切都感觉很笨拙并且需要做很多工作。这是正确的做法吗?

  • 是否有其他方法可以在主窗口事件循环中放置一个函数来监视前面描述的队列并采取行动?或者我应该深入了解 .NET 细节并开始使用 .NET 事件或调度程序吗?

最佳答案

我自己的大部分问题都找到了答案。

  • 我认为我们不能假设 DLL 是线程安全的,因此最好将与 DLL 的所有交互隔离到一个线程。

  • 看起来 Windows 消息系统是按线程而不是按进程的,所以是的,每个使用 Windows 消息系统的线程都需要有一个 Windows 消息处理循环。

  • 可以使用 Form.Invoke 在 Windows 事件循环中插入执行。运行非 ui 主循环,您可以使用 Dispatcher.CurrentDispatcher 获取调度程序,它可用于“调用”函数。然后调用的函数在创建调度程序的线程上执行。被调用的函数需要被委托(delegate),这是 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()

一些相关链接:

关于c# - Python .NET、多线程和 Windows 事件循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45753171/

相关文章:

c# - 在异常期间捕获原始 HTTP POST 数据

python - 如何在 python 3.5 中以毫秒而不是微秒获取日期时间的 ISO8601 字符串

objective-c - 顶点数组对象 (VAO) 能否在 OpenGL ES 中的 EAGLContext 之间共享?

c# - Unity Share 脚本 Android 到 iOS

c# - 在 Visual Studio 2017 安装项目中添加卸载时的自定义操作

c# - SetPixel 太慢了。有没有更快的方法来绘制位图?

python - 显示具有相同颜色条的子图

python-dev安装报错: ImportError: No module named apt_pkg

java - 为什么死锁没有发生?

worker 中的 Python uwsgi 线程