python 代码中的线程错误?

Threading errors in python code?

所以我正在编写一个代码,用于实时映射一个 csv 文件的数据集。数据的形式为 ("string",integer)。最初它必须是 运行 作为两个不同的程序,所以我想将它们和 运行 作为一个程序进行线程化。代码是:

import _thread
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
import csv
import time

def firstfunc(a):
    count=0
    with open('airtabletest.txt','r') as dataset:
        line=csv.reader(dataset)
        arr=[]
        for row in line:
            if(len(arr)>=9):
                arr.clear()
            for i in range(1,10):
                arr.append(int(row[i]))
            t=time.time()+3
            while(t>time.time()):
                    pass
            if(count>=8):
                with open('live_graph1','r') as file:
                    lines=file.readlines()
                with open('live_graph1','w') as csvfile:
                    csvfile.writelines(lines[1:])
            with open('live_graph1','a+') as file:
                arr2=[]
                writer=csv.writer(file)
                arr2.append(row[0][11:])
                arr2.append(sum(arr)/10)
                writer.writerow(arr2)
                count+=1

def secondfunc(b):
    style.use('fivethirtyeight')

    fig = plt.figure()
    ax1 = fig.add_subplot(1,1,1)
    def animate(i):
        xs = []
        ys = []
        count=0
        label=[]
        with open('live_graph1','r') as file:
            reader=csv.reader(file)
            for row in reader:
                if(len(row) == 2):
                    x = float(row[1])
                    xs.append(count)
                    ys.append(x)
                    label.append(row[0])
                    count+=1
            ax1.clear()
            ax1.set_xticks(xs)
            ax1.set_xticklabels(label)
            ax1.plot(xs,ys)
            fig.autofmt_xdate()
    ani = animation.FuncAnimation(fig, animate, interval=1000)
    plt.show()

def main():
    try:
        _thread.start_new_thread(secondfunc,(Thread2,))
        _thread.start_new_thread(firstfunc,(tHREAD1,))
    except RuntimeError:
        pass

    while 1:
        pass

if __name__=='__main__':main()

我得到的错误是:

  File "test.py", line 65
    while 1:
        ^
SyntaxError: invalid syntax

即使暂时假设我删除了while循环,那么错误是:

  File "test.py", line 66
    if __name__=='__main__':main()
    ^
IndentationError: unexpected unindent

据我所知,语法没问题。谁能解释一下我做错了什么? 编辑:正如一些答案所建议的,我添加了 except 语句。然后它在 _thread.start_new_thread 函数中要求 2 个参数。这样做之后,我收到此错误:

Traceback (most recent call last):
  File "test.py", line 71, in <module>
    if __name__=='__main__':main()
  File "test.py", line 63, in main
    _thread.start_new_thread(secondfunc,(Thread2,))
NameError: name 'Thread2' is not defined

所以问题确实是在 except 调整之后,你在一个线程中调用函数而不传递任何参数,因为线程需要参数和函数才能工作,否则它会抛出只需要两个给定一个参数的错误所以为了解决这个问题,如果我们的函数不带参数,我们可以将一个空元组作为 arg 传递。

那么

_thread.start_new_thread(firstfunc,)

现在是这个

_thread.start_new_thread(firstfunc,())

这应该有效

导入_thread 将 matplotlib.pyplot 导入为 plt 导入 matplotlib.animation 作为动画 从 matplotlib 导入样式 导入 csv 导入时间

def firstfunc():
    count=0
    with open('airtabletest.txt','r') as dataset:
        line=csv.reader(dataset)
        arr=[]
        for row in line:
            if(len(arr)>=9):
                arr.clear()
            for i in range(1,10):
                arr.append(int(row[i]))
            t=time.time()+3
            while(t>time.time()):
                    pass
            if(count>=8):
                with open('live_graph1','r') as file:
                    lines=file.readlines()
                with open('live_graph1','w') as csvfile:
                    csvfile.writelines(lines[1:])
            with open('live_graph1','a+') as file:
                arr2=[]
                writer=csv.writer(file)
                arr2.append(row[0][11:])
                arr2.append(sum(arr)/10)
                writer.writerow(arr2)
                count+=1

def secondfunc():
    style.use('fivethirtyeight')

    fig = plt.figure()
    ax1 = fig.add_subplot(1,1,1)
    def animate(i):
        xs = []
        ys = []
        count=0
        label=[]
        with open('live_graph1','r') as file:
            reader=csv.reader(file)
            for row in reader:
                if(len(row) == 2):
                    x = float(row[1])
                    xs.append(count)
                    ys.append(x)
                    label.append(row[0])
                    count+=1
            ax1.clear()
            ax1.set_xticks(xs)
            ax1.set_xticklabels(label)
            ax1.plot(xs,ys)
            fig.autofmt_xdate()
    ani = animation.FuncAnimation(fig, animate, interval=1000)
    plt.show()

def main():
    try:
        _thread.start_new_thread(secondfunc,())
        _thread.start_new_thread(firstfunc,())
    except RuntimeError as e:
        print(e)
    while 1:
        pass

if __name__=='__main__':main()

为什么您的代码出现语法错误是因为您需要指定如果 try 失败该怎么办。即必须指定 except:

我也相信你的代码 while 1 总是 return 是真的,它似乎什么也没做

添加如何

except RuntimeError:
    pass

或尝试后类似的东西?