Python线程全局变量问题



我有几个脚本想同时运行,它们读取CSV文件,我正在尝试以下操作;

import sys
import csv
out = open("C:PYDUMPPYDUMPINST.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()

这被保存为takefile1.py。我有20个具有类似结构的脚本,我想同时运行。因此,我使用(我一直在使用它来运行其他批次的脚本)以下内容;

import csv
import sys
from threading import Thread

def firstsend_lot():
        execfile("C:Userstakefile1.py")                                    
        execfile("C:Userstakefile2.py") 

def secondsend_lot():  

        execfile("C:Userstakefile3.py")                                    
        execfile("C:Userstakefile4.py") 

if __name__ == '__main__':
    Thread(target = firstsend_lot).start()
    Thread(target = secondsend_lot).start()

所以我得到错误"全局名称'take'没有定义"。有人有什么建议吗?我对Python非常绝望,所以假装你在和一个白痴说话。

您的函数example()没有访问权限。尝试在其中添加一行:

def example():
      global take
      try:
          lfo = int(take)
          if lfo > 0:
          #code
      except Exception, e:
          pass  

最新更新