asyncio matplotlib show() 仍然冻结程序
asyncio matplotlib show() still freezes program
我希望运行模拟,同时在绘图中输出其进度。我一直在查看大量线程和多处理的示例,但它们都非常复杂。所以我认为使用 Python 的新 asyncio
库应该更容易。
我找到了一个示例 () 并根据我的原因对其进行了修改:
import matplotlib.pyplot as plt
import asyncio
import numpy as np
class DataAnalysis():
def __init__(self):
# asyncio so we can plot data and run simulation in parallel
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(self.plot_reward())
finally:
loop.run_until_complete(
loop.shutdown_asyncgens()) # see: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.shutdown_asyncgens
loop.close()
async def async_generator(self):
for i in range(3):
await asyncio.sleep(.4)
yield i * i
async def plot_reward(self):
# Prepare the data
x = np.linspace(0, 10, 100)
# Plot the data
plt.plot(x, x, label='linear')
#plt.show()
# add lines to plot
async for i in self.async_generator():
print(i)
# Show the plot
plt.show()
if __name__ == '__main__':
DataAnalysis()
问题
我添加了一个简单的 plt.show()
程序仍然死机。我想 asyncio
我可以 运行 并行吗?显然我的知识仍然缺乏。
执行以下操作的示例将非常有用:
- 每次
async_generator
returns 一个值时,在(matplotlib
的)图中添加一条线。
首先,我误解了asyncio,它不会让运行事情并行化()。
似乎唯一对我有用的是 plt.pause(0.001)
()。 plt.draw()
打开了一个 window,但它没有显示任何内容,plt.show
冻结了程序。似乎 plt.show(block=False)
已被弃用,使用 plt.ion
给出了程序完成时最终结果关闭的问题。还有await asyncio.sleep(0.1)
没有让情节划清界限。
工作代码
import matplotlib.pyplot as plt
import asyncio
import matplotlib.cbook
import warnings
warnings.filterwarnings("ignore",category=matplotlib.cbook.mplDeprecation)
class DataAnalysis():
def __init__(self):
# asyncio so we can plot data and run simulation in parallel
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(self.plot_reward())
finally:
loop.run_until_complete(
loop.shutdown_asyncgens()) # see: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.shutdown_asyncgens
loop.close()
# keep plot window open
plt.show()
async def async_generator(self):
for i in range(3):
await asyncio.sleep(.4)
yield i * i
async def plot_reward(self):
#plt.ion() # enable interactive mode
# receive dicts with training results
async for i in self.async_generator():
print(i)
# update plot
if i == 0:
plt.plot([2, 3, 4])
elif i == 1:
plt.plot([3, 4, 5])
#plt.draw()
plt.pause(0.1)
#await asyncio.sleep(0.4)
if __name__ == '__main__':
da = DataAnalysis()
备注
但是您会收到一条已弃用的消息:python3.6/site-packages/matplotlib/backend_bases.py:2445: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented
warnings.warn(str, mplDeprecation)
,您可以通过以下方式抑制该消息:warnings.filterwarnings()
.
我不确定 asyncio
对于我的用例是否真的有必要...
threading
和 multiprocessing
之间的区别感兴趣的人:Multiprocessing vs Threading Python
我希望运行模拟,同时在绘图中输出其进度。我一直在查看大量线程和多处理的示例,但它们都非常复杂。所以我认为使用 Python 的新 asyncio
库应该更容易。
我找到了一个示例 (
import matplotlib.pyplot as plt
import asyncio
import numpy as np
class DataAnalysis():
def __init__(self):
# asyncio so we can plot data and run simulation in parallel
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(self.plot_reward())
finally:
loop.run_until_complete(
loop.shutdown_asyncgens()) # see: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.shutdown_asyncgens
loop.close()
async def async_generator(self):
for i in range(3):
await asyncio.sleep(.4)
yield i * i
async def plot_reward(self):
# Prepare the data
x = np.linspace(0, 10, 100)
# Plot the data
plt.plot(x, x, label='linear')
#plt.show()
# add lines to plot
async for i in self.async_generator():
print(i)
# Show the plot
plt.show()
if __name__ == '__main__':
DataAnalysis()
问题
我添加了一个简单的 plt.show()
程序仍然死机。我想 asyncio
我可以 运行 并行吗?显然我的知识仍然缺乏。
执行以下操作的示例将非常有用:
- 每次
async_generator
returns 一个值时,在(matplotlib
的)图中添加一条线。
首先,我误解了asyncio,它不会让运行事情并行化(
似乎唯一对我有用的是 plt.pause(0.001)
(plt.draw()
打开了一个 window,但它没有显示任何内容,plt.show
冻结了程序。似乎 plt.show(block=False)
已被弃用,使用 plt.ion
给出了程序完成时最终结果关闭的问题。还有await asyncio.sleep(0.1)
没有让情节划清界限。
工作代码
import matplotlib.pyplot as plt
import asyncio
import matplotlib.cbook
import warnings
warnings.filterwarnings("ignore",category=matplotlib.cbook.mplDeprecation)
class DataAnalysis():
def __init__(self):
# asyncio so we can plot data and run simulation in parallel
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(self.plot_reward())
finally:
loop.run_until_complete(
loop.shutdown_asyncgens()) # see: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.shutdown_asyncgens
loop.close()
# keep plot window open
plt.show()
async def async_generator(self):
for i in range(3):
await asyncio.sleep(.4)
yield i * i
async def plot_reward(self):
#plt.ion() # enable interactive mode
# receive dicts with training results
async for i in self.async_generator():
print(i)
# update plot
if i == 0:
plt.plot([2, 3, 4])
elif i == 1:
plt.plot([3, 4, 5])
#plt.draw()
plt.pause(0.1)
#await asyncio.sleep(0.4)
if __name__ == '__main__':
da = DataAnalysis()
备注
但是您会收到一条已弃用的消息:
python3.6/site-packages/matplotlib/backend_bases.py:2445: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented warnings.warn(str, mplDeprecation)
,您可以通过以下方式抑制该消息:warnings.filterwarnings()
.我不确定
asyncio
对于我的用例是否真的有必要...threading
和multiprocessing
之间的区别感兴趣的人:Multiprocessing vs Threading Python