简体   繁体   English

Python创建C线程

[英]Python creating C threads

I'm trying to create a C camera module that python can interact with. 我正在尝试创建一个Python可以与之交互的C相机模块。 The way I envision it working is: 我设想它的工作方式是:

  • Python callable C methods to setup the camera (eg set gain, set trigger, etc) Python可调用的C方法来设置相机(例如,设置增益,设置触发器等)
  • a C thread is started somewhere polling/called back with frames, when a frame is received in the thread - it is sent to a socket. 当在线程中接收到帧时,C线程将在轮询/调用帧的某处启动-将其发送到套接字。

Basically Python will call the C methods to which will pass messages to the C thread. 基本上,Python将调用C方法,将消息传递给C方法。 I'll make it thread safe using C mutexes or semaphores to pass the control command over to the C thread. 我将使用C互斥量或信号量将控制命令传递给C线程,使其成为线程安全的。

The question is where do I start the C thread? 问题是我在哪里启动C线程? Is it best to start it in the PyMODINIT_FUNC or should I have another function to start the thread which returns an object holding a reference to the C thread or is there a better simple way? 最好是在PyMODINIT_FUNC中启动它,还是应该有另一个函数来启动线程,该线程返回一个持有对C线程的引用的对象,或者有更好的简单方法吗? I need something reasonable quick to implement. 我需要一些可以快速实施的东西。

Cheers. 干杯。 Mitch. 米奇。

Ended up just starting the thread in a "start" function and storing the thread id in a static variable and stopping the thread in a "stop" method. 最后只是在“启动”函数中启动线程,然后将线程ID存储在静态变量中,然后在“停止”方法中停止线程。 Works fine, raises errors where appropriate - no issues. 工作正常,在适当的地方引发错误-没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM