I have a large engineering simulation code that is set up to send data via a TCP socket. I have written a C++ library that reads from the socket, like so:

bool read_next(monitor_buffer_object* obj){    int valread = 1;    valread = read(server_fd, buffer, buffer_size);    if (valread <= 0) return false;    return obj->try_build_from_bytes(buffer, 0, buffer_size);}

This function has a Python wrapper that looks like this:

static PyObject* bitcartinterlib_ReceiveObject(PyObject *self, PyObject *args){    PyObject* output = Py_BuildValue("");    external_monitor::monitor_buffer_object received_object;    bool result = external_monitor::read_next(&received_object);    if (result)    {        output = build_py_buffer_obj(&received_object);    }    received_object.dispose();    return output;}

It should be noted that these functions are blocking.

Now, I have written a Tkinter GUI in Python that calls on_tick in a separate thread on the main thread.

def on_tick(self):    data_in = myCustomPythonModule.ReceiveObject()    if data_in is not None:        print(data_in)    else:        self.host.has_valid_connection = False

Note that self.host is an object containing all the GUI event code, and the invocation of on_tick depends on self.host.has_valid_connection such that the logic prevents an indefinite lock.

However, the simulation code transmits these objects at a somewhat unpredictable rate. For example, say 1 object per second.

2020欧洲杯手机版注册The problem is that the rest of the GUI is unusable for the time that it takes for the simulation code to transmit an object. This is no surprise thanks to the GIL.

2020欧洲杯手机版注册I am wondering if there is an easy way around this? It would seem that my only option right now it to either make the read non-blocking or to handle the multithreading in C++, but I would like to do the threading in the GUI application for the sake of simplicity.

  • "way around this? ": If this is True: calls on_tick in a separate thread, you don't show it, use Process instead of Thread. – stovfl yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.