Mocking a subprocess call in Python

It seems unusual to me that you use the patch decorator over the run_script function, since you don’t pass a mock argument there. How about this: from unittest import mock import subprocess def run_script(file_path): process = subprocess.Popen([“myscript”, -M, file_path], stdout=subprocess.PIPE) output, err = process.communicate() return process.returncode @mock.patch(“subprocess.Popen”) def test_run_script(self, mock_subproc_popen): process_mock = mock.Mock() attrs = … Read more

Subprocess cp returns error – bufsize must be integer [duplicate]

You’ve got two problems: 1 – subprocess.call() expects a list of arguments, not multiple arguments: #wrong subprocess.call(‘cp’, in, out) #right subprocess.call([‘cp’, in, out]) 2 – in is a python reserved keyword, change it to something like inp: #wrong subprocess.call([‘cp’, in, out]) #right subprocess.call([‘cp’, inp, out])

subprocess.Popen() error (No such file or directory) when calling command with arguments as a string

You should pass the arguments as a list (recommended): subprocess.Popen([“wc”, “-l”, “sorted_list.dat”], stdout=subprocess.PIPE) Otherwise, you need to pass shell=True if you want to use the whole “wc -l sorted_list.dat” string as a command (not recommended, can be a security hazard). subprocess.Popen(“wc -l sorted_list.dat”, shell=True, stdout=subprocess.PIPE) Read more about shell=True security issues here. In this case, … Read more

Python subprocess.Popen() wait for completion [duplicate]

Use Popen.wait: process = subprocess.Popen([“your_cmd”]…) process.wait() Or check_output, check_call which all wait for the return code depending on what you want to do and the version of python. If you are using python >= 2.7 and you don’t care about the output just use check_call. You can also use call but that will not raise … Read more

How do i use subprocesses to force python to release memory?

The important thing about the optimization suggestion is to make sure that my_function() is only invoked in a subprocess. The deepcopy and del are irrelevant — once you create five million distinct integers in a process, holding onto all of them at the same time, it’s game over. Even if you stop referring to those … Read more

Communicate multiple times with a process without breaking the pipe?

I think you misunderstand communicate… http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate communicate sends a string to the other process and then waits on it to finish… (Like you said waits for the EOF listening to the stdout & stderror) What you should do instead is: proc.stdin.write(‘message’) # …figure out how long or why you need to wait… proc.stdin.write(‘message2’) (and if … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)