Ruby exec 缓存 python 脚本响应

Ruby exec caches python script responses

我使用 exec 从 Ruby 执行 Python 如下:

Ruby

array = ['first string', 'second string']
array.each { |s| 
  result = exec("python print_string.py -s '#{s}'")
  puts result
}

Python

import argparse

parser = argparse.ArgumentParser(description='Print string app')
parser.add_argument('-s', '--strg', type=str, help='shows string')
args = parser.parse_args()

print(args.strg)

2 个问题:

你做错了。 exec 的文档,强调我的:

Replaces the current process by running the given external command...

你想要 system,它只是将命令作为子进程运行。