OSError: [WinError87] The parameter is invalid

I have a python script written using python 3.4.3 and it inserts a CSV file of ip addresses, usernames and passwords to go to another batch of script.

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")

The file (autorun-input.csv) that it reads is as follows:

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

It does not work on multiple Windows 7 machines, the error is this:

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

I don’t understand which one is parameterwrong. It seems that the exception is thrown at p.open.

+4
source share
1 answer

this type of error is usually caused by subprocess passing. Open a command without an executable file. eg:

subprocess.Popen(' -s') # notice the space at the beginning
subprocess.Popen(['','-s']) # this will cause the same error as well

check your log (you wrote the 'command' variable in your log before the error) to see if it is incorrect for any reason

, , , . infact, ( python2.7), , .

- :

with open('tempLog{0}.log'.format(os.getpid(),'w+') as f:
    subprocess.Popen(command, stdout=f, stderr=f)

,

+1

Source: https://habr.com/ru/post/1605768/


All Articles