#python #multithreading
#python #многопоточность
Вопрос:
В моем скрипте при его запуске я получаю следующее сообщение об ошибке:
Exception in thread Thread-1:
Traceback (most recent call last):
File "D:Python27libthreading.py", line 801, in __bootstrap_inner
self.run()
File "D:Python27libthreading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "D:DPSpolling_DO_EEM_RPP01_v3_test.py", line 721, in cloneInit
processRawOnly(dupName, dupDict, raw, last, stop, rawName, rawDict, x, y)
UnboundLocalError: local variable 'processRawOnly' referenced before assignment
Упомянутая здесь локальная переменная, однако, является не переменной, а функцией. Я проверил, не объявил ли я случайно переменную с тем же именем, и я этого не сделал.
Функция, которая создает поток, приведена ниже:
def startCloneThread():
threads = []
try:
if any('ems' in file for file in os.listdir(Incoming_Data)):
print('Clone EMS')
tEMS=threading.Thread(target = cloneInit, args = (suffixesEMS, suffixes2EMS, suffixes3EMS, initListEMS, initCSVEMS, ctfEMS, gccEMS, rawEMS, csvEMS, lastEMS, stopEMS, runDictEMS, csvDictEMS, rawDictEMS, dupDictEMS, runNameEMS, csvNameEMS, rawNameEMS, dupNameEMS))
threads.append(tEMS)
if any('study1' in file for file in os.listdir(Incoming_Data)):
print('Clone STUDY1')
tSTDY1=threading.Thread(target = cloneInit, args = (suffixesStdy1, suffixes2Stdy1, suffixes3Stdy1, initListStdy1, initCSVStdy1, ctfStdy1, gccStdy1, rawStdy1, csvStdy1, lastStdy1, stopStdy1, runDictStdy1, csvDictStdy1, rawDictStdy1, dupDictStdy1, runNameStdy1, csvNameStdy1, rawNameStdy1, dupNameStdy1))
threads.append(tSTDY1)
if any('study2' in file for file in os.listdir(Incoming_Data)):
print('Clone STUDY2')
tSTDY2=threading.Thread(target = cloneInit, args = (suffixesStdy2, suffixes2Stdy2, suffixes3Stdy2, initListStdy2, initCSVStdy2, ctfStdy2, gccStdy2, rawStdy2, csvStdy2, lastStdy2, stopStdy2, runDictStdy2, csvDictStdy2, rawDictStdy2, dupDictStdy2, runNameStdy2, csvNameStdy2, rawNameStdy2, dupNameStdy2))
threads.append(tSTDY2)
if any('study3' in file for file in os.listdir(Incoming_Data)):
print('Clone STUDY3')
tSTDY3=threading.Thread(target = cloneInit, args = (suffixesStdy3, suffixes2Stdy3, suffixes3Stdy3, initListStdy3, initCSVStdy3, ctfStdy3, gccStdy3, rawStdy3, csvStdy3, lastStdy3, stopStdy3, runDictStdy3, csvDictStdy3, rawDictStdy3, dupDictStdy3, runNameStdy3, csvNameStdy3, rawNameStdy3, dupNameStdy3))
threads.append(tSTDY3)
if any('study4' in file for file in os.listdir(Incoming_Data)):
print('Clone STUDY4')
tSTDY4=threading.Thread(target = cloneInit, args = (suffixesStdy4, suffixes2Stdy4, suffixes3Stdy4, initListStdy4, initCSVStdy4, ctfStdy4, gccStdy4, rawStdy4, csvStdy4, lastStdy4, stopStdy4, runDictStdy4, csvDictStdy4, rawDictStdy4, dupDictStdy4, runNameStdy4, csvNameStdy4, rawNameStdy4, dupNameStdy4))
threads.append(tSTDY4)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
except:
ScriptRunTime = runTime(ScriptStartTime)
RunTimeMessage = traceback.print_exc()
logging.warning(RunTimeMessage)
Ошибка выдается всякий раз, когда processRawOnly()
вызывается, она отлично работает, когда
lookForProcessingFiles()
вызывается. Я попытался изменить порядок вызова обеих функций, но это не имело никакого значения.
Обе функции определены выше как cloneInit()
, так и startCloneThread()
.
У кого-нибудь есть представление о том, почему processRawOnly()
вызывает UnboundLocalError?
Функция, в которой возникает ошибка, приведена ниже:
def cloneInit(suffixes, suffixes2, suffixes3, initList, initCSV, ctf, gcc, raw, csv, last, stop, runDict, csvDict, rawDict, dupDict, runName, csvName, rawName, dupName):
if any((file.endswith(suffixes2) or file.endswith(raw))for file in os.listdir(Incoming_Data)):
time.sleep(5)
ScriptRunTime = runTime(ScriptStartTime)
RunTimeMessage = 'I am cloneInit() - Run Time: ' str(ScriptRunTime)
logging.warning(RunTimeMessage)
if any((file.endswith(suffixes2))for file in os.listdir(Incoming_Data)):
for file in os.listdir(Incoming_Data):
if file.endswith(gcc) or file.endswith(ctf):
initList.append(file)
print(initList)
if len(initList) >= 2: #if items exist in list
for x,y in itertools.combinations(initList,2):
if (x[:48]) == (y[:48]) and x.endswith(ctf) and y.endswith(gcc):
if not any((file.endswith(csv))for file in os.listdir(Incoming_Data)):
moveInitFilesRaw(dupName, dupDict, rawName, rawDict, x, y)
processRawOnly(dupName, dupDict, raw, last, stop, rawName, rawDict, x, y)
else:
moveInitFilesRun(dupName, dupDict, runName, runDict, x, y)
lookForProcessingFiles(dupName, dupDict, raw, csv, last, stop, runName, runDict, x, y)
del initList[:]
elif len(initList) <= 1 :
for file in initList:
if file.endswith(ctf):
if any(f.endswith(csv) for f in os.listdir(Incoming_Data)):
moveInitFilesRun(dupName, dupDict, runName, runDict, file)
lookForProcessingFiles(dupName, dupDict, raw, csv, last, stop, runName, runDict, file)
else:
moveInitFilesRaw(dupName, dupDict, rawName, rawDict, file)
processRawOnly(dupName, dupDict, raw, last, stop, rawName, rawDict, file)
elif file.endswith(gcc):
if any((file.endswith(csv))for file in os.listdir(Incoming_Data)):
moveInitFilesRun(dupName, dupDict, runName, runDict, file)
lookForProcessingFiles(dupName, dupDict, raw, csv, last, stop, runName, runDict, file)
else:
moveInitFilesRaw(dupName, dupDict, rawName, rawDict, file)
processRawOnly(dupName, dupDict, raw, last, stop, rawName, rawDict, file)
del initList[:]
elif not any((file.endswith(suffixes))for file in os.listdir(Incoming_Data)):
for file in os.listdir(Incoming_Data):
if file.endswith(raw) and (len(runDict) == 0) and (len(csvDict) == 0):
if (len(file) == 84):
rawFile = file[:32] file[64:-4] '_chkRaw1'
else:
rawFile = file[:32] file[62:-4] '_chkRaw1'
if rawFile not in rawDict:
rawDict[rawFile] = ''
dictSave(rawName, rawDict)
processRawOnly = (dupName, dupDict, raw, last, stop, rawName, rawDict, rawFile )
elif not any((file.endswith(suffixes2))for file in os.listdir(Incoming_Data)):
for file in os.listdir(Incoming_Data):
if file.endswith(raw) or file.endswith(csv) and (len(runDict) == 0):
if (len(runDict) == 0):
initCSV.append(file)
if initCSV and (len(runDict) == 0 and len(rawDict) == 0): #if items exist in list
for a,b in itertools.combinations(initCSV,2):
if (a[:32]) == (b[:32]) and (a[64:-4] == b[64:-22] or a[62:-4] == b[62:-22]) and a.endswith(raw) and b.endswith(csv):
if (len(a) == 84):
rawBase = a[:32] a[64:-4] '_chkRaw1'
else:
rawBase = a[:32] a[62:-4] '_chkRaw1'
if (len(b) == 102):
csvBase = b[:32] b[64:-22] '_chkCSV1'
else:
csvBase = b[:32] b[62:-22] '_chkCSV1'
if rawBase not in csvDict:
csvDict[rawBase] = ''
dictSave(csvName, csvDict)
if csvBase not in csvDict:
csvDict[csvBase] = ''
dictSave(csvName, csvDict)
lookForProcessingFiles(dupName, dupDict, raw, csv, last, stop, csvName, csvDict,rawBase,csvBase)
del initCSV[:]
else:
if any((file.endswith(stop))for file in os.listdir(Incoming_Data)):
for f in os.listdir(Incoming_Data):
if f.startswith(stop):
shutil.move(Incoming_Data f, DPT_INPUT_ST f)
Комментарии:
1. Не ответ, но
processRawOnly = (dupName, dupDict, raw, . . .
должен бытьprocessRawOnly(dupName, dupDict, raw, . . .
?2. @Carcigenicate два дня, и я продолжал пропускать это, спасибо, что это было причиной моих проблем!!
3. О, хорошо. Рад, что помогло.