婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 可視化工具PyVista多線程顯示多窗口的實例代碼

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

熱門標簽:400電話申請服務商選什么 江蘇客服外呼系統廠家 清遠360地圖標注方法 在哪里辦理400電話號碼 西藏智能外呼系統五星服務 平頂山外呼系統免費 千陽自動外呼系統 工廠智能電話機器人 原裝電話機器人

在使用PyVista進行多線程同時顯示多個窗口的時候,發現開啟多個線程顯示窗口,窗口會卡死,于是便有了這篇文章。

發現問題

在可視化工具——利用PyVista進行mesh的色彩映射這篇博客中,我們實現了使用四種方法對mesh進行色彩映射,為了對這四種方法映射結果有一個直觀的認識,我第一個想法就是開啟四個線程,分別調用這四個函數。
代碼如下:
定義四個色彩映射函數:

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: 比較數據的名字
 :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進行色彩映射
  :param mesh: 輸入mesh
  :param name: 比較數據的名字
  :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):
 """
  使用進行plot自帶的色彩映射
  :param mesh: 輸入mesh
  :param name: 比較數據的名字
  :return:
 """
 pts = mesh.points
 mesh[name] = pts[:, 1]
 mesh.plot(scalars=name, cmap='viridis_r')


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

開啟四個線程調用:

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()

結果,卡頓了

問題分析

首先說一下python的多線程問題

python多線程

線程(Thread)也叫輕量級進程,是操作系統能夠進行運算調度的最小單位,它被包涵在進程之中,是進程中的實際運作單位。線程自己不擁有系統資源,只擁有一點兒在運行中必不可少的資源,但它可與同屬一個進程的其它線程共享進程所擁有的全部資源。一個線程可以創建和撤消另一個線程,同一進程中的多個線程之間可以并發執行。

舉個簡單的例子來理解下:
假定有一 7 * 24 小時不停工的工廠,由于其電力有限,一次僅供一個車間使用,當一個車間在生產時,其他車間停工。在這里我們可以理解這個工廠相當于操作系統,供電設備相當于 CPU,一個車間相當于一個進程。

一個車間里,可以有很多工人。他們協同完成一個任務。車間的空間是工人們共享的,這里一個工人就相當于一個線程,一個進程可以包括多個線程。比如許多房間是每個工人都可以進出的。這象征一個進程的內存空間是共享的,每個線程都可以使用這些共享內存。

Python 多線程適合用在 I/O 密集型任務中。I/O 密集型任務較少時間用在 CPU 計算上,較多時間用在 I/O 上,如文件讀寫,web 請求,數據庫請求 等;而對于計算密集型任務,應該使用多進程。

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

問題解決

很明顯,應該使用多進程來顯示四個窗口。
代碼:

if __name__ == '__main__':
 #mesh = pv.read('vtkData/airplane.ply')
 mesh = examples.download_embryo()
 # 開啟多進程用于可視化曲面
 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()

結果:

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

您可能感興趣的文章:
  • C語言實現四窗口聊天
  • Java 滑動窗口最大值的實現
  • JavaScript實現鼠標控制自由移動的窗口
  • C#仿QQ聊天窗口
  • Pyqt5 實現窗口縮放,控件在窗口內自動伸縮的操作
  • Android窗口小部件基礎編寫代碼實例
  • Python爬蟲之Selenium多窗口切換的實現
  • Java窗口精細全方位講解

標簽:股票 西安 隨州 錦州 天水 日照 安慶 白城

巨人網絡通訊聲明:本文標題《可視化工具PyVista多線程顯示多窗口的實例代碼》,本文關鍵詞  可視化,工具,PyVista,多,線程,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《可視化工具PyVista多線程顯示多窗口的實例代碼》相關的同類信息!
  • 本頁收集關于可視化工具PyVista多線程顯示多窗口的實例代碼的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 灵寿县| 桓仁| 左云县| 陵水| 惠东县| 南城县| 大姚县| 青州市| 饶阳县| 天气| 延吉市| 厦门市| 上饶市| 房山区| 临泽县| 项城市| 灌南县| 沧源| 桑日县| 敦化市| 泾阳县| 鹤岗市| 新闻| 西峡县| 潢川县| 安岳县| 合江县| 东山县| 五莲县| 崇阳县| 新巴尔虎右旗| 沭阳县| 鄂托克旗| 怀来县| 兴化市| 太白县| 五常市| 修水县| 苏尼特左旗| 阜康市| 蓝田县|