【问题标题】:Why does Python refuse to execute this code in a new subprocess?为什么 Python 拒绝在新的子进程中执行这段代码?
【发布时间】:2015-07-13 20:25:55
【问题描述】:

我正在尝试制作一个非常简单的应用程序,允许人们在应用程序中定义自己的小 Python 脚本。我想在新进程中执行代码,以便以后轻松杀死。不幸的是,Python 一直给我以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "/home/skylion/Documents/python_exec test.py", line 19, in <module>
    code_process = Process(target=exec_, args=(user_input_code))
  File "/usr/lib/python2.7/multiprocessing/process.py", line 104, in __init__
    self._args = tuple(args)
TypeError: 'code' object is not iterable
>>> 

我的代码贴在下面

user_input_string = '''
import os
world_name='world'
robot_name='default_body + os.path.sep'
joint_names=['hingejoint0', 'hingejoint1', 'hingejoint2', 'hingejoint3', 'hingejoint4', 'hingejoint5', 'hingejoint6', 'hingejoint7', 'hingejoint8']
print(joint_names)
'''

def exec_(arg):
    exec(arg)
user_input_code = compile(user_input_string, 'user_defined', 'exec') 
from multiprocessing import Process
code_process = Process(target=exec_, args=(user_input_code))
code_process.start()    

我错过了什么?我的 user_input_string 有问题吗?用我的编译选项?任何帮助将不胜感激。

【问题讨论】:

    标签: python concurrency compilation subprocess


    【解决方案1】:

    我相信args 一定是一个元组。要创建单元素元组,请添加一个逗号,如下所示:args=(user_input_code,)

    【讨论】:

      猜你喜欢
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 2016-05-16
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多