Python线程全局变量问题
我有几个脚本,我想同时运行,他们读取一个CSV文件,我尝试以下Python线程全局变量问题,python,multithreading,global-variables,Python,Multithreading,Global Variables,我有几个脚本,我想同时运行,他们读取一个CSV文件,我尝试以下 import sys import csv out = open("C:\PYDUMP\PYDUMPINST.csv","r") dataf=csv.reader(out) for row in dataf: take = row[0] give = row[1] def example(): try: lfo = int(take) if lfo
import sys
import csv
out = open("C:\PYDUMP\PYDUMPINST.csv","r")
dataf=csv.reader(out)
for row in dataf:
take = row[0]
give = row[1]
def example():
try:
lfo = int(take)
if lfo > 0:
#code
except Exception, e:
pass
example()
import csv
import sys
from threading import Thread
def firstsend_lot():
execfile("C:\Users\takefile1.py")
execfile("C:\Users\takefile2.py")
def secondsend_lot():
execfile("C:\Users\takefile3.py")
execfile("C:\Users\takefile4.py")
if __name__ == '__main__':
Thread(target = firstsend_lot).start()
Thread(target = secondsend_lot).start()
这将另存为takefile1.py。我有20个类似结构的脚本,我想同时运行。因此,我使用我一直用于运行其他批次的脚本无故障以下
import sys
import csv
out = open("C:\PYDUMP\PYDUMPINST.csv","r")
dataf=csv.reader(out)
for row in dataf:
take = row[0]
give = row[1]
def example():
try:
lfo = int(take)
if lfo > 0:
#code
except Exception, e:
pass
example()
import csv
import sys
from threading import Thread
def firstsend_lot():
execfile("C:\Users\takefile1.py")
execfile("C:\Users\takefile2.py")
def secondsend_lot():
execfile("C:\Users\takefile3.py")
execfile("C:\Users\takefile4.py")
if __name__ == '__main__':
Thread(target = firstsend_lot).start()
Thread(target = secondsend_lot).start()
因此,我得到的错误是没有定义全局名称“take”。有人有什么建议吗?我对Python非常失望,所以假装你在和一个白痴说话。你的函数示例,没有获取的权限。尝试在其中添加一行:
def example():
global take
try:
lfo = int(take)
if lfo > 0:
#code
except Exception, e:
pass
非常好,伊瓦罗,谢谢你的时间,非常感谢。一个简单的示例可以更好地显示10页python帮助页面。无论是谁设计了python帮助,都一定是在发笑。@user3439187,如果你喜欢这个回答,就直接投票吧,伙计。上面写着超过10个“谢谢你”@Konstantin,我已经试过了,但是我需要更多的分数来投票给某人,我需要15分,我只有2分。所以,谢谢你,恐怕现在还得做些什么。你为什么不把我的问题投票给我,给我打分呢当然,我开玩笑。@user3439187:我很高兴这个解决方案起到了作用。你不能对这个问题投赞成票,但你可以批准我的答案。这就是系统的工作原理。通过同意我的回答,你的声誉也提高了。实际上这是一个僵局。在这个世界上,只有你可以通过“批准”来批准任何有效的答案,我的意思是点击数字下方的符号来接受答案。