Why does Python refuse to execute this code in a new subprocess?
Question: I am trying to make a very simple application that allows for people to define
their own little python scripts within the application. I want to execute the
code in a new process to make it easy to kill later. Unfortunately, Python
keeps giving me the following error:

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

My code is posted below

    
    
    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()    
    

What am I missing? Is there something wrong with my user_input_string? With my
compile options? Any help would be appreciated.


Answer: I believe `args` must be a tuple. To create a single-element tuple, add a
comma like so: `args=(user_input_code,)`

