在C ++中嵌入matplotlib

时间:2012-04-11 16:05:38

标签: c++ python matplotlib

我正在从带有C ++代码的套接字读取消息,并尝试使用matplotlib以交互方式绘制它,但似乎Python代码将阻止主线程,无论我使用show()还是{ {1}}和ion()draw()ion()不会在Python中阻止。

知道如何在C ++代码中以draw()交互式绘图吗?

一个例子真的很好。

非常感谢。

1 个答案:

答案 0 :(得分:6)

您也可以尝试创建一个新的线程来执行调用 阻塞功能,因此它不会阻止主程序中的IO 环。使用一个线程对象数组并循环查找未使用的 一,创建一个线程来执行阻塞调用,并有另一个线程 在他们完成时加入他们。

这段代码是一个快速的共同点,我做了一些来证明我的意思 使用线程来获取阻塞函数的伪异步行为...... 我没有编译它或梳理它很好,它只是显示 你是如何做到这一点的。

#include <pthread.h>
#include <sys/types.h>
#include <string>
#include <memory.h>
#include <malloc.h>
#define MAX_THREADS 256 // Make this as low as possible!
using namespace std;
pthread_t PTHREAD_NULL;
typedef string someTypeOrStruct;
class MyClass
{
    typedef struct
    {
        int id;
        MyClass *obj;
        someTypeOrStruct input;
    } thread_data;

    void draw();    //Undefined in this example
    bool getInput(someTypeOrStruct *);  //Undefined in this example
    int AsyncDraw(MyClass * obj, someTypeOrStruct &input);
    static void * Joiner(MyClass * obj);
    static void * DoDraw(thread_data *arg);
    pthread_t thread[MAX_THREADS], JoinThread;
    bool threadRunning[MAX_THREADS], StopJoinThread;

    bool exitRequested;
public:
    void Main();
};

bool MyClass::getInput(someTypeOrStruct *input)
{
}

void MyClass::Main()
{
    exitRequested = false;
    pthread_create( &JoinThread, NULL, (void *(*)(void *))MyClass::Joiner, this);

    while(!exitRequested)
    {
        someTypeOrStruct tmpinput;
        if(getInput(&tmpinput))
            AsyncDraw(this, tmpinput);
    }

    if(JoinThread != PTHREAD_NULL)
    {
        StopJoinThread = true;
        pthread_join(JoinThread, NULL);
    }
}

void *MyClass::DoDraw(thread_data *arg)
{
    if(arg == NULL) return NULL;
    thread_data *data = (thread_data *) arg;
    data->obj->threadRunning[data->id] = true;
    // -> Do your draw here <- //
    free(arg);
    data->obj->threadRunning[data->id] = false; // Let the joinThread know we are done with this handle...
}

int MyClass::AsyncDraw(MyClass *obj, someTypeOrStruct &input)
{
    int timeout = 10; // Adjust higher to make it try harder...
    while(timeout)
    {
        for(int i = 0; i < MAX_THREADS; i++)
        {
            if(thread[i] == PTHREAD_NULL)
            {
                thread_data *data = (thread_data *)malloc(sizeof(thread_data));
                if(data)
                {
                    data->id = i;
                    data->obj = this;
                    data->input = input;

                    pthread_create( &(thread[i]), NULL,(void* (*)(void*))MyClass::DoDraw, (void *)&data);
                    return 1;
                }
                return 0;
            }
        }
        timeout--;
    }
}

void *MyClass::Joiner(MyClass * obj)
{
    obj->StopJoinThread = false;
    while(!obj->StopJoinThread)
    {
        for(int i = 0; i < MAX_THREADS; i++)
            if(!obj->threadRunning[i] && obj->thread[i] != PTHREAD_NULL)
            {
                pthread_join(obj->thread[i], NULL);
                obj->thread[i] = PTHREAD_NULL;
            }
    }
}

int main(int argc, char **argv)
{
    MyClass base;
    base.Main();
    return 0;
}

这样,您可以在绘制过程中继续接受输入。

~~修正了以上代码实际编译,确保添加-lpthread