因此,我正在编写用于对csv文件的数据集进行实时映射的代码。数据的格式为(“字符串”,整数)。最初,它必须作为两个不同的程序运行,因此我想将它们都线程化并作为一个程序运行。代码是:
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
据我所知,语法还可以。谁能解释我做错了什么吗? 编辑:作为一些答案建议,我添加了除外声明。然后它在_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
答案 0 :(得分:1)
所以问题出在除调整之后,实际上是您在线程中调用函数而没有传递任何参数,因为线程需要参数和函数都可以工作,否则会抛出仅需要两个给定一个arg的错误,因此顺序修复如果我们的函数不带参数的话,我们可以将一个空元组作为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 失败时的处理方法。即必须指定除外:
我也相信您的代码中只有1个总是返回true,并且似乎根本没有做任何事情
答案 1 :(得分:0)
如何添加
except RuntimeError:
pass
还是尝试后类似的东西?