简体   繁体   English

在C ++中嵌入matplotlib

[英]Embedding matplotlib in C++

I am reading a message from a socket with C++ code and am trying to plot it interactively with matplotlib , but it seems Python code will block the main thread, no matter I use show() or ion() and draw() . 我正在从带有C ++代码的套接字读取消息,并且我试图用matplotlib以交互方式绘制它,但似乎Python代码将阻止主线程,无论我使用show()还是ion()draw() ion() and draw() won't block in Python. ion()draw()不会在Python中阻塞。

Any idea how to plot interactively with matplotlib in C++ code? 知道如何用C ++代码中的matplotlib交互式绘图吗?

An example would be really good. 一个例子真的很好。

Thanks a lot. 非常感谢。

You may also try creating a new thread that does the call to the blocking function, so that it does not block IO in your main program loop. 您也可以尝试创建一个调用阻塞函数的新线程,这样它就不会阻止主程序循环中的IO。 Use an array of thread objects and loop through to find an unused one, create a thread to do the blocking calls, and have another thread that joins them when they are completed. 使用一个线程对象数组并循环查找未使用的对象,创建一个线程来执行阻塞调用,并让另一个线程在完成时加入它们。

This code is a quick slap-together I did to demonstrate what I mean about using threads to get pseudo asynchronous behavior for blocking functions... I have not compiled it or combed over it very well, it is simply to show you how to accomplish this. 这个代码是一个快速的共同点我在一起演示了我使用线程获取阻塞函数的伪异步行为的意思...我没有编译它或很好地梳理它,它只是告诉你如何完成这个。

#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;
}

This way you can continue accepting input while the draw is occurring. 这样,您可以在绘制过程中继续接受输入。

~~Fixed so the above code actually compiles, make sure to add -lpthread ~~修正了以上代码实际编译,确保添加-lpthread

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

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