0

I have a python program listening to windows messages in a loop. Getting some live data from this loop.

Loop syntax https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessagew

BOOL bRet;

   while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
    { 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
}

Now when i want to show this data i use this code block to refresh plot in a callback

def callback(value,item): 


x.append(value)
y.append(datetime.now())

plt.clf()
plt.plot(y,x)
plt.draw()
plt.pause(0.0001)

Every time i move mouse over plot(or do anything about the window actually) it triggers my loop code. Also resize gives this error.

Fatal Python error: PyEval_RestoreThread: NULL tstate Python runtime state: initialized

How can i stop this? I tried to use threads but failed to share data to the thread to show the plot. Not even sure if this will help

Edit: giving plt.pause() a bigger number then callbacks intervals does exactly what i want but if number is very big like "4294967" then sleeps stacks and crash program very quickly. If number is small like 5 and callback is not called in that time windows starts registering mouse which leads to a crash with same error if i move mouse over window.

Edit2: Using matplotlib.use("Qt5agg") removes most of my problems and resize work.But still fires mouse events and some more everytime callback called. Also since this backend prevents plot from stealing focus but makes it flashing every half seconds which also triggers windows message loop.

Edit3:Opening figures options(or any other button of plot) does what i want. How can i make sure main window of plot also does the same?

Xentios
  • 84
  • 1
  • 10
  • Using matplotlib.use("Qt5agg") removes most of my problems and resize work.But still fires mouse events and some more evertime callback called. – Xentios Jun 02 '20 at 22:57
  • 1
    Hi, Did you call function `callback` in the [WinProc](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms633573(v=vs.85))? Then the window will call `callback` when each message comes including `WM_MOUSEMOVE` and `WM_SIZE`. – Drake Wu Jun 03 '20 at 05:45
  • *Getting some live data from this loop.* Which data you want to get? If you want to refresh your plt regularly, you could use [`SetTimer`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-settimer) to create a timer, and then handle the [`WM_TIMER`](https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-timer) message in the `WinProc`(to call the `callback` in the WM_TIMER) or call `callback` function in [`TIMERPROC`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nc-winuser-timerproc) directly. – Drake Wu Jun 03 '20 at 05:46
  • @Drake Wu - MSFT it is prob something like that some library may be doing something like that. The part i don't get is when i open some other windows from the plot it stops. My code still gets live data but does not get any other message. – Xentios Jun 03 '20 at 18:40
  • I refresh the data and plot when my callback gets the data. Does not it make more sense? callback is fired randomly. – Xentios Jun 03 '20 at 18:42
  • Since I cannot reproduce your problem yet, could you please provide a [MCVE](https://stackoverflow.com/help/minimal-reproducible-example), which will allow us to better help you solve the problem. – Drake Wu Jun 05 '20 at 03:18
  • Thank you for your interest, real code is very large and other systems dependent. Even if i give you all code it wont work in your computer. As long as i understand "GetMessage( &msg, hWnd, 0, 0 )" part gets every windows message based on your process id. I don't know how first window of plot uses same id but prob other options of plot opens on another thread with a different id or someother variable i don't know. Most importantly current behavior does not broke my code and it is ok as long as it does not block live data, so far it looks like it does not. – Xentios Jun 05 '20 at 09:35

0 Answers0