Is it possible to run function in a subprocess without threading or writing a separate file/script.
I think you're looking for something more like the multiprocessing module:
http://docs.python.org/library/multiprocessing.html#the-process-class
The subprocess module is for spawning processes and doing things with their input/output - not for running functions.
Here is a multiprocessing
version of your code:
from multiprocessing import Process, Queue
# must be a global function
def my_function(q, x):
q.put(x + 100)
if __name__ == '__main__':
queue = Queue()
p = Process(target=my_function, args=(queue, 1))
p.start()
p.join() # this blocks until the process terminates
result = queue.get()
print result
How to run second Python script from first one?
Try this:
process = subprocess.Popen([r'app_virtual_helper.bat'])
So that your script doesn't wait for the call on your second script to end
Call a function defined in a shell script from a subprocess
You should read the file with source
or just .
then invoke function. Source will load bash script defined functions.
The right way will be source my_scripts.sh && func1
.
EDIT:
I came with this solution.
bash
#!/bin/bash
function func1() {
echo "func1 completed"
}
function invoke_python(){
python a.py
}
if [ -z "$@" ]; then
invoke_python
else
"$@"
fi
and for python I got that.
import subprocess
p = subprocess.Popen("./a.sh func1",
stdout = subprocess.PIPE,
shell = True)
p.wait()
Launch a python script from another script, with parameters in subprocess argument
The subprocess
library is interpreting all of your arguments, including demo_oled_v01.py
as a single argument to python. That's why python is complaining that it cannot locate a file with that name. Try running it as:
p = subprocess.Popen(['python', 'demo_oled_v01.py', '--display',
'ssd1351', '--width', '128', '--height', '128', '--interface', 'spi',
'--gpio-data-command', '20'])
See more information on Popen here.
Constantly print Subprocess output while process is running
You can use iter to process lines as soon as the command outputs them: lines = iter(fd.readline, "")
. Here's a full example showing a typical use case (thanks to @jfs for helping out):
from __future__ import print_function # Only Python 2.x
import subprocess
def execute(cmd):
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
for stdout_line in iter(popen.stdout.readline, ""):
yield stdout_line
popen.stdout.close()
return_code = popen.wait()
if return_code:
raise subprocess.CalledProcessError(return_code, cmd)
# Example
for path in execute(["locate", "a"]):
print(path, end="")
Related Topics
How to Install Pip for Python 2.6
How to Check Whether a Variable Is a Class or Not
How to Erase the File Contents of Text File in Python
Python Webdriver to Handle Pop Up Browser Windows Which Is Not an Alert
Same Output in Different Workers in Multiprocessing
Attributeerror: 'Pandasexprvisitor' Object Has No Attribute 'Visit_Ellipsis', Using Pandas Eval
Py_Initialize/Py_Finalize Not Working Twice with Numpy
How to Print a Dictionary's Key
How to Switch Columns Rows in a Pandas Dataframe
How to Print the Key-Value Pairs of a Dictionary in Python
Command Executed with Paramiko Does Not Produce Any Output
No Multiline Lambda in Python: Why Not
Why Can't I Repeat the 'For' Loop for CSV.Reader