简体   繁体   中英

Run python function from command line or subprocess popen

I wanted to run a test function called test.counttest() that counts up to 10.

def counttest():
    x = 0
    for x in range(0,3):
        x = x+1
        print("Number: "+ str(x))
        time.sleep(1)

I want to call just the function from the command line OR from subprocess popen. Not write the function, just call it. Everything I have google keeps bringing me back to how I can write a function from the command line which is NOT what I need.

I need to specifically run a function from subprocess popen so I can get the stdout in a forloop that can then be sent to a flask socket. (This is required)

Main point - How can Call (not write) a function from the command line or from subprocess?

Not this:

python -c 'import whatever then add code'

But something like this:

python "test.counttest()"

or like this:

subprocess.Popen(['python', ".\test.counttest()"],stdout=subprocess.PIPE, bufsize=1,universal_newlines=True)

EDIT: This is for @Andrew Holmgren. Consider the following script:

def echo(ws):
    data = ws.receive()
    with subprocess.Popen(['powershell', ".\pingtest.ps1"],stdout=subprocess.PIPE, bufsize=1,universal_newlines=True) as process:
            for line in process.stdout:
                line = line.rstrip()
                print(line)
                try:
                    ws.send(line+ "\n")
                except:
                    pass

this works perfectly for what I need as it: takes the script's stdout and send's it to the ws.send() function which is a websocket.

However I need this same concept for a function instead. The only way I know how to get the stdout easily is from using subprocess.popen but if there is another way let me know. This is why I am trying to make a hackjob way of running a function through the subprocess module.

The question of Run python function from command line or subprocess popen relates in the fact that if I can get a function to run from subprocess, then I know how to get the stdout for a websocket.

Actually you have really a lot of questions inside this one.

  1. How can I send output of function line-by-line to another one (and/or websocket)? Just avoid writing to stdout and communicate directly. yield (or other generator creation methods) are intended exatly for that.
import time

def counttest():
    for i in range(10):
        yield f'Item {i}'
        time.sleep(1)

def echo(ws):
    # data = ws.receive()
    for row in counttest():
        ws.send(row)
  1. How to call a function func_name defined in file (suppose it's test.py ) from command line? Being in directory with test.py , do
$ python -c 'from test import func_name; func_name()'
  1. How to read from sys.stdout ? The easiest will be to replace it with io.StringIO and restore thing back later.
from contextlib import redirect_stdout
import io

def echo(ws):
    f = io.StringIO()
    with redirect_stdout(f):
        counttest()
    output = f.getvalue()
    ws.send(output)

It will return only after call_function() , so you cannot monitor real-time printed items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM