最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

可視化工具PyVista多線程顯示多窗口的實(shí)例代碼

 更新時(shí)間:2021年04月06日 09:30:23   作者:薛定貓  
這篇文章主要介紹了可視化工具PyVista多線程顯示多窗口,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在使用PyVista進(jìn)行多線程同時(shí)顯示多個(gè)窗口的時(shí)候,發(fā)現(xiàn)開啟多個(gè)線程顯示窗口,窗口會(huì)卡死,于是便有了這篇文章。

發(fā)現(xiàn)問(wèn)題

在可視化工具——利用PyVista進(jìn)行mesh的色彩映射這篇博客中,我們實(shí)現(xiàn)了使用四種方法對(duì)mesh進(jìn)行色彩映射,為了對(duì)這四種方法映射結(jié)果有一個(gè)直觀的認(rèn)識(shí),我第一個(gè)想法就是開啟四個(gè)線程,分別調(diào)用這四個(gè)函數(shù)。
代碼如下:
定義四個(gè)色彩映射函數(shù):

import pyvista as pv
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np
import colorcet
import threading
from pyvista.demos import demos
from pyvista import examples
import multiprocessing

def mesh_cmp_custom(mesh, name):
 """
 自定義色彩映射
 :param mesh: 輸入mesh
 :param name: 比較數(shù)據(jù)的名字
 :return:
 """
 pts = mesh.points
 mesh[name] = pts[:, 1]
 # Define the colors we want to use
 blue = np.array([12 / 256, 238 / 256, 246 / 256, 1])
 black = np.array([11 / 256, 11 / 256, 11 / 256, 1])
 grey = np.array([189 / 256, 189 / 256, 189 / 256, 1])
 yellow = np.array([255 / 256, 247 / 256, 0 / 256, 1])
 red = np.array([1, 0, 0, 1])

 c_min = mesh[name].min()
 c_max = mesh[name].max()
 c_scale = c_max - c_min

 mapping = np.linspace(c_min, c_max, 256)
 newcolors = np.empty((256, 4))
 newcolors[mapping >= (c_scale * 0.8 + c_min)] = red
 newcolors[mapping < (c_scale * 0.8 + c_min)] = grey
 newcolors[mapping < (c_scale * 0.55 + c_min)] = yellow
 newcolors[mapping < (c_scale * 0.3 + c_min)] = blue
 newcolors[mapping < (c_scale * 0.1 + c_min)] = black

 # Make the colormap from the listed colors
 my_colormap = ListedColormap(newcolors)
 mesh.plot(scalars=name, cmap=my_colormap)


def mesh_cmp_mpl(mesh, name):
 """
  使用Matplotlib進(jìn)行色彩映射
  :param mesh: 輸入mesh
  :param name: 比較數(shù)據(jù)的名字
  :return:
  """
 pts = mesh.points
 mesh[name] = pts[:, 1]
 mlp_cmap = plt.cm.get_cmap("viridis", 25)
 mesh.plot(scalars=name, cmap=mlp_cmap)


def mesh_cmp(mesh, name):
 """
  使用進(jìn)行plot自帶的色彩映射
  :param mesh: 輸入mesh
  :param name: 比較數(shù)據(jù)的名字
  :return:
 """
 pts = mesh.points
 mesh[name] = pts[:, 1]
 mesh.plot(scalars=name, cmap='viridis_r')


def mesh_cmp_colorcet(mesh, name):
 """
  使用進(jìn)行colorcet進(jìn)行色彩映射
  :param mesh: 輸入mesh
  :param name: 比較數(shù)據(jù)的名字
  :return:
 """
 pts = mesh.points
 mesh[name] = pts[:, 1]
 mesh.plot(scalars=name, cmap=colorcet.fire)

開啟四個(gè)線程調(diào)用:

if __name__ == '__main__':
 #mesh = pv.read('vtkData/airplane.ply')
 mesh = examples.download_embryo()
 # 開啟多線程用于可視化曲面
 t1 = threading.Thread(target=mesh_cmp, args=(mesh, 'y_height',))
 t1.start()
 t2 = threading.Thread(target=mesh_cmp_mpl, args=(mesh, 'y_height',))
 t2.start()
 t3 = threading.Thread(target=mesh_cmp_custom, args=(mesh, 'y_height',))
 t3.start()

 t1.join()
 t2.join()
 t3.join()

結(jié)果,卡頓了

在這里插入圖片描述

問(wèn)題分析

首先說(shuō)一下python的多線程問(wèn)題

python多線程

線程(Thread)也叫輕量級(jí)進(jìn)程,是操作系統(tǒng)能夠進(jìn)行運(yùn)算調(diào)度的最小單位,它被包涵在進(jìn)程之中,是進(jìn)程中的實(shí)際運(yùn)作單位。線程自己不擁有系統(tǒng)資源,只擁有一點(diǎn)兒在運(yùn)行中必不可少的資源,但它可與同屬一個(gè)進(jìn)程的其它線程共享進(jìn)程所擁有的全部資源。一個(gè)線程可以創(chuàng)建和撤消另一個(gè)線程,同一進(jìn)程中的多個(gè)線程之間可以并發(fā)執(zhí)行。

舉個(gè)簡(jiǎn)單的例子來(lái)理解下:
假定有一 7 * 24 小時(shí)不停工的工廠,由于其電力有限,一次僅供一個(gè)車間使用,當(dāng)一個(gè)車間在生產(chǎn)時(shí),其他車間停工。在這里我們可以理解這個(gè)工廠相當(dāng)于操作系統(tǒng),供電設(shè)備相當(dāng)于 CPU,一個(gè)車間相當(dāng)于一個(gè)進(jìn)程。

一個(gè)車間里,可以有很多工人。他們協(xié)同完成一個(gè)任務(wù)。車間的空間是工人們共享的,這里一個(gè)工人就相當(dāng)于一個(gè)線程,一個(gè)進(jìn)程可以包括多個(gè)線程。比如許多房間是每個(gè)工人都可以進(jìn)出的。這象征一個(gè)進(jìn)程的內(nèi)存空間是共享的,每個(gè)線程都可以使用這些共享內(nèi)存。

Python 多線程適合用在 I/O 密集型任務(wù)中。I/O 密集型任務(wù)較少時(shí)間用在 CPU 計(jì)算上,較多時(shí)間用在 I/O 上,如文件讀寫,web 請(qǐng)求,數(shù)據(jù)庫(kù)請(qǐng)求 等;而對(duì)于計(jì)算密集型任務(wù),應(yīng)該使用多進(jìn)程。

參考: https://blog.csdn.net/somezz/article/details/80963760

問(wèn)題解決

很明顯,應(yīng)該使用多進(jìn)程來(lái)顯示四個(gè)窗口。
代碼:

if __name__ == '__main__':
 #mesh = pv.read('vtkData/airplane.ply')
 mesh = examples.download_embryo()
 # 開啟多進(jìn)程用于可視化曲面
 p1 = multiprocessing.Process(target=mesh_cmp_custom, args=(mesh, 'y_height',))
 p2 = multiprocessing.Process(target=mesh_cmp_mpl, args=(mesh, 'y_height',))
 p3 = multiprocessing.Process(target=mesh_cmp, args=(mesh, 'y_height',))
 p1.start()
 p2.start()
 p3.start()

 p1.join()
 p2.join()
 p3.join()

結(jié)果:

在這里插入圖片描述

到此這篇關(guān)于可視化工具PyVista多線程顯示多窗口的實(shí)例代碼的文章就介紹到這了,更多相關(guān)PyVista可視化工具內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python常用擴(kuò)展插件使用教程解析

    Python常用擴(kuò)展插件使用教程解析

    這篇文章主要介紹了Python常用擴(kuò)展插件使用教程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • python中關(guān)于xmltodict的使用

    python中關(guān)于xmltodict的使用

    這篇文章主要介紹了python中關(guān)于xmltodict的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 詳解Python中的List 2

    詳解Python中的List 2

    這篇文章主要為大家介紹了Python中的List,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • 最新評(píng)論

    合阳县| 漳浦县| 安庆市| 吴川市| 洛南县| 拉孜县| 双流县| 江永县| 宜丰县| 宁陵县| 昔阳县| 磴口县| 千阳县| 永春县| 游戏| 文山县| 宜宾县| 临邑县| 宁蒗| 页游| 定兴县| 天柱县| 峨山| 深州市| 乐亭县| 扬中市| 双城市| 金门县| 德惠市| 沈阳市| 微博| 镇安县| 安乡县| 泰顺县| 承德市| 镇康县| 江油市| 河西区| 大冶市| 吉水县| 额济纳旗|