为什么 Python 拒绝在新的子进程中执行这段代码?
Why does Python refuse to execute this code in a new subprocess?
我正在尝试制作一个非常简单的应用程序,允许人们在应用程序中定义他们自己的小 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 有问题吗?使用我的编译选项?任何帮助将不胜感激。
我相信 args
一定是一个元组。要创建单元素元组,请像这样添加一个逗号:args=(user_input_code,)
我正在尝试制作一个非常简单的应用程序,允许人们在应用程序中定义他们自己的小 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 有问题吗?使用我的编译选项?任何帮助将不胜感激。
我相信 args
一定是一个元组。要创建单元素元组,请像这样添加一个逗号:args=(user_input_code,)