我有一个使用python 3.4.3编写的python脚本,它会输入一个ip地址,用户名和密码的.csv文件,以传递给另一个批处理脚本。
import pdb
import csv
import os
import subprocess
import datetime
import time
import signal
from multiprocessing import Process
def callGetDimmBatchFile(logFile, batchFileName, ipAddress, userName, passWord):
print('\nId: {0}'.format(counter) + '\n', file=logFile, flush=True)
command ='{0} -i {1} -u {2} -p {3}'.format(batchFileName, ipAddress, userName, passWord)
print(command, file=logFile, flush=True)
print("IP Address is {0}".format(ipAddress))
print("User name is {0}".format(userName))
print("Password is {0}".format(passWord))
timeout = 60
start = datetime.datetime.now()
process = subprocess.Popen(command, stdout=logFile, stderr=logFile)
while process.poll() is None:
time.sleep(0.1)
now = datetime.datetime.now()
if (now - start).seconds > timeout:
process.kill()
# os.kill(process.pid, signal.SIGKILL)
# os.waitpid(-1, os.Warning)
return None
rc = process.wait()
print('\nReturn Code:', rc, file=logFile, flush=True)
logFile = open('log.txt', 'w+')
batchFileName = 'getfoo.bat'
pathToCsv = 'autorun-input.csv'
print('Path to CSV is {0}'.format(pathToCsv))
counter = 0
with open(pathToCsv) as csvFile:
reader = csv.reader(csvFile, delimiter=',')
for row in reader:
ipAddress = row[0]
userName = row[1]
passWord = row[2]
p = Process(target=callGetDimmBatchFile, args=(logFile, batchFileName, ipAddress, userName, passWord))
p.start()
p.join()
#callGetDimmBatchFile(logFile, batchFileName, ipAddress, userName, passWord)
os.system("pause")
它读入的文件(autorun-input.csv)是:
10.69.69.1,taclab,taclab
10.69.69.2,taclab,taclab
10.69.69.3,taclab,taclab
10.69.69.4,taclab,taclab
10.69.69.5,taclab,taclab
10.69.69.6,taclab,taclab
10.69.69.7,taclab,taclab
10.69.69.8,taclab,taclab
10.69.69.9,taclab,taclab
10.69.69.10,taclab,taclab
它无法在多台Windows 7计算机上运行,错误如下:
C:\Users\Taclab\Desktop\DimmScript\Python-Project-9\Python-Project\DimmReport>python autorun.py
Path to CSV is autorun-input.csv
Traceback (most recent call last):
File "autorun.py", line 44, in <module>
p.start()
File "C:\Python34\lib\multiprocessing\process.py", line 105, in start
self._popen = self._Popen(self)
File "C:\Python34\lib\multiprocessing\context.py", line 212, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Python34\lib\multiprocessing\context.py", line 313, in _Popen
return Popen(process_obj)
File "C:\Python34\lib\multiprocessing\popen_spawn_win32.py", line 66, in __ini
t__
reduction.dump(process_obj, to_child)
File "C:\Python34\lib\multiprocessing\reduction.py", line 59, in dump
ForkingPickler(file, protocol).dump(obj)
TypeError: cannot serialize '_io.TextIOWrapper' object
C:\Users\Taclab\Desktop\DimmScript\Python-Project-9\Python-Project\DimmReport>Tr
aceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python34\lib\multiprocessing\spawn.py", line 100, in spawn_main
new_handle = steal_handle(parent_pid, pipe_handle)
File "C:\Python34\lib\multiprocessing\reduction.py", line 81, in steal_handle
_winapi.PROCESS_DUP_HANDLE, False, source_pid)
OSError: [WinError 87] The parameter is incorrect
我不明白哪个parameter
不正确。看来异常是在p.open
抛出的。
答案 0 :(得分:3)
这种类型的错误通常是由传递subprocess.Popen没有可执行文件的命令引起的。例如:
subprocess.Popen(' -s') # notice the space at the beginning
subprocess.Popen(['','-s']) # this will cause the same error as well
检查你的日志(你在错误之前将变量'command'写入你的日志)以查看它是否因某种原因无效
如果可以,那么我猜它必须是日志文件,因为父进程打开它。 事实上,如果我尝试在我的电脑上做同样的事情(我在python2.7上尝试过它)它引发了一个不同的错误,关于日志文件已经关闭。
尝试做类似的事情:
with open('tempLog{0}.log'.format(os.getpid(),'w+') as f:
subprocess.Popen(command, stdout=f, stderr=f)
并查看是否有效
答案 1 :(得分:0)
我也遇到过这种情况,但解决方法是我没有让子进程继承 SYSTEMROOT
环境变量。