Why matplotlib requires to plot only in the main thread?

前端 未结 2 1887
闹比i
闹比i 2021-01-20 15:58

I\'m trying to plot live the output of a generator.

The following code works as expected (Ctrl-C terminates execution):

import numpy as np
import pyl         


        
2条回答
  •  粉色の甜心
    2021-01-20 16:17

    Why does matplotlib care which thread does the plotting? I'm not asking how to solve this but rather why is this happening in the first place.

    @Evert is right, it's not matplotlib, it's your GUI toolkit (one of the backends that matplotlib uses to create a window with a plot for you). It happens because GUI toolkits are event-driven (you don't want blocking behavior for the user interface, right?) and they have internal event loop, that controls program execution. The idea is that events are monitored by the event loop and dispatched to the callbacks. To do this, event loop should be started in the main thread, while callbacks for long-running tasks are moved to separate threads.

提交回复
热议问题