Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

list - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...