简体   繁体   中英

Running python program with arguments from another program

I want to call a python program from current program,

def multiply(a):
    return a*5
mul = sys.argv[1]

I saved this file as test.py .so from the current file I'm calling it but i want to run the code in parallel like multiprocessing queue,But its not working. what I tried so far,

import os
import sys
import numpy as np
cwd = os.getcwd()
l =  [20,5,12,24]
for i in np.arange(len(l)):
    os.system('python test.py multiply[i]')

I want to run the main script for all list items parallelly like multiprocessing. How to achieve that?

If you want to make your program work like that using that the os.system, you need to change the test.py file a bit:

import sys

def multiply(a):
    return a*5

n = int(sys.argv[1])
print(multiply(n))

This code written like this takes the second element (the first one is just the name of the file) of argv that it took when you executed it with os.system and converted it to an integer, then executed your function (multiply by 5).

In these cases though, it's way better to just import this file as a module in your project.

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