python - 高 CPU 负载(来自其他应用程序)会影响 python 性能/准确性吗?

标签 python

我正在编写代码来读取和显示有限元分析 (FEA) 计算的结果。结果存储在几个(相对较大的)文本文件中,其中包含节点列表(ID 号、空间位置)和相关物理字段列表(节点 ID、该点字段的值)。

但是,我注意到,当我在后台运行 FEA 案例并尝试同时运行我的代码时,它会返回错误,并不总是相同的错误,也不总是在相同的迭代中,一切似乎都很好随机且不对代码或输入文件进行任何修改,只需在运行之间间隔几秒钟点击运行按钮即可。

我遇到的错误示例是:

keys[key] = np.round(np.asarray(keys[key]),7)
TypeError: can't multiply sequence by non-int of type 'float'

#-------------------------------------------------------------------------

triang = tri.Triangulation(x, y)
ValueError: x and y arrays must have a length of at least 3

#-------------------------------------------------------------------------

line = [float(n) for n in line]
ValueError: could not convert string to float: '0.1225471E'

如果您好奇的话,这是我的代码(请记住,它还没有完成,我是一名机械工程师,而不是程序员)。也欢迎任何关于如何让它变得更好的反馈:

import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
import os

triangle_max_radius = 0.003
respath = 'C:/path'
fields = ['TEMPERATURE']

# Plot figure definition --------------------------------------------------------------------------------------
fig, ax1 = plt.subplots()
fig.subplots_adjust(left=0, right=1, bottom=0.04, top=0.99)
ax1.set_aspect('equal')
# ------------------------------------------------------------------------------------------------------------- 

# Read outputfiles --------------------------------------------------------------------------------------------
resfiles = [f for f in os.listdir(respath) if (os.path.isfile(os.path.join(respath,f)) and f[:3]=='csv')]
resfiles = [[f,int(f[4:])] for f in resfiles]
resfiles = sorted(resfiles,key=lambda x: (x[1]))
resfiles = [os.path.join(respath,f[:][0]).replace("\\","/") for f in resfiles]
# ------------------------------------------------------------------------------------------------------------- 

# Read data inside outputfile ---------------------------------------------------------------------------------    
for result_file in resfiles:
    keys = {}
    keywords = []
    with open(result_file, 'r') as res:
        for line in res:
            if line[0:2] == '##':
                if len(line) >= 5:
                    line = line[:3] + line[7:]
            line = line.replace(';',' ')
            line = line.split()
            if line:
                if line[0] == '##':
                    if len(line) >= 3:
                        keywords.append(line[1])
                        keys[line[1]] = []
                elif line[0] in keywords:
                    curr_key = line[0]
                else:
                    line = [float(n) for n in line]
                    keys[curr_key].append(line)

    for key in keys:
        keys[key] = np.round(np.asarray(keys[key]),7)

    for item in fields:
        gob_temp = np.empty((0,4))
        for node in keys[item]:
            temp_coords, = np.where(node[0] == keys['COORDINATES'][:,0])
            gob_temp_coords = [node[0], keys['COORDINATES'][temp_coords,1], keys['COORDINATES'][temp_coords,2], node[1]]
            gob_temp = np.append(gob_temp,[gob_temp_coords],axis=0)

        x = gob_temp[:,1]
        y = gob_temp[:,2]
        z = gob_temp[:,3]
        triang = tri.Triangulation(x, y)

        triangles = triang.triangles
        xtri = x[triangles] - np.roll(x[triangles], 1, axis=1)
        ytri = y[triangles] - np.roll(y[triangles], 1, axis=1)
        maxi = np.max(np.sqrt(xtri**2 + ytri**2), axis=1)
        triang.set_mask(maxi > triangle_max_radius)

        ax1.tricontourf(triang,z,100,cmap='plasma')
        ax1.triplot(triang,color="black",lw=0.2)

plt.show()

回到问题,python 的准确性/性能是否可能受到 CPU 负载或任何其他“外部”因素的影响?或者这不是一个选项,我的代码肯定有问题(顺便说一下,它在其他情况下运行良好)?

最佳答案

没有,其他进程只影响how often your process gets time slots to execute -- 即,从用户的角度来看,完成工作的速度。

如果您在负载下遇到错误,这意味着您的程序逻辑中存在错误——最有可能的是,race conditions . 它们基本上归结为对您的环境做出假设,当其中存在其他事件时,这些假设不再成立。例如:

  • 您的程序是多线程的,逻辑会假设线程的执行顺序。(这包括假设完成某些任务需要多长时间。)
  • 您的程序正在使用其他进程也在同时使用的共享资源(文件、流等)。 (例如,当您尝试读取文件时,其他一些程序正在(覆盖)写入文件。或者,如果您正在从流中读取,则并非所有数据都可用。)

关于python - 高 CPU 负载(来自其他应用程序)会影响 python 性能/准确性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52503266/

相关文章:

python - 如何检查 python 字符串以查看它是否在 HH :MM time format? 中

python - Pandas Google Datareader 仅返回 1 年的股票数据

Python zip() 函数

python - 如何使用 Django 显示毫秒而不是微秒

python - 使用自定义层保存 Keras 模型

Python Pandas 无法识别/t 分隔符

python - 删除 Python Pandas 中多列中的所有重复行

python - 如果条件为 true,Scrapy 获取 href 值

python - 如何将列表列表转换为第一个元素是索引,第二个是列名的数据框

python - 如何限制函数调用的执行时间?