我有3个python脚本,一个是调度程序,一个是多线程的,另一个是用于多处理的。我试图借助调度程序执行多处理脚本。当我使用多线程而不是多处理时,程序可以工作。我不知道是什么问题。以下是用于多线程和多处理的脚本。我正在Windows中使用Python3.6进行所有操作。我只是在scheduler.py中将multiprocessing.py替换为multithreading.py,并且程序可以正常工作。
scheduler.py
def job():
try :
exec(open('multiprocessing.py').read())
except Exception as e:
print(e)
schedule.every(100).seconds.do(job)
while True:
schedule.run_pending()
multiprocessing.py
for i in range(cores):
start = i*singleLength
processName = 'Process-'+str(i+1)
if (len(linesList) / 2 == 0 ) and (i == cores - 1):
end = singleLength + (i*singleLength)
values = (linesList[start:end],processName)
elif (len(linesList) / 2 != 0) and (i == cores - 1):
#semiListLength = len(linesList[start:])
values = (linesList[start:],processName)
else:
end = singleLength + (i*singleLength)
#semiListLength = len(linesList[start:end])
values = (linesList[start:end],processName)
processList.append(multiprocessing.Process(target = FTMain.parsing, args = values))
for i in range(cores):
processList[i].start()
for i in range(cores):
processList[i].join()
multithreading.py
for i in range(cores):
start = i*singleLength
processName = 'Thread-'+str(i+1)
if (len(linesList) / 2 == 0 ) and (i == cores - 1):
end = singleLength + (i*singleLength)
#semiListLength = len(linesList[start:end])
values = (linesList[start:end],processName)
elif (len(linesList) / 2 != 0) and (i == cores - 1):
#semiListLength = len(linesList[start:])
values = (linesList[start:],processName)
else:
end = singleLength + (i*singleLength)
#semiListLength = len(linesList[start:end])
values = (linesList[start:end],processName)
processList.append(threading.Thread(target = FTMainTemp.parsing, args = values))
for i in range(cores):
processList[i].start()
for i in range(cores):
processList[i].join()
以下是我在运行multiprocessing.py时遇到的错误
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.