def _run(self, fn: Callable, *args, **kwargs):
        thread_id = threading.get_ident()
        interp_id, chan_id = self.interpreters[thread_id]
        fn_bytes = pickle.dumps((fn, args, kwargs),
                                protocol=pickle.HIGHEST_PROTOCOL)

        interpreters.run_string(
            interp_id,
            dedent("""
                fn, args, kwargs = pickle.loads(fn_bytes)
                retval_obj = fn(*args, **kwargs)
                retval_bytes = pickle.dumps(
                    retval_obj,
                    protocol=pickle.HIGHEST_PROTOCOL
                )
                interpreters.channel_send(chan_id, retval_bytes)
                """),
            shared={
                "fn_bytes": fn_bytes,
                "chan_id": chan_id
            },
        )

        retval_bytes = interpreters.channel_recv(chan_id)
        return pickle.loads(retval_bytes)
 def _initializer(self,
                  user_initializer: Callable[..., None] = None,
                  *initargs: Any) -> None:
     """Create a sub-interpreter and optionally run user-defined
     initializer function inside it
     """
     thread_id = threading.get_ident()
     interp_id = interpreters.create()
     chan_id = interpreters.channel_create()
     self.interpreters[thread_id] = interp_id, chan_id
     if user_initializer:
         init_bytes = pickle.dumps((user_initializer, initargs),
                                   protocol=pickle.HIGHEST_PROTOCOL)
         interpreters.run_string(
             interp_id,
             dedent("""
                 import pickle
                 import _xxsubinterpreters as interpreters
     
                 initializer, initargs = pickle.loads(init_bytes)
                 initializer(*initargs)
                 """),
             shared={"init_bytes": init_bytes},
         )
     else:
         interpreters.run_string(
             interp_id,
             dedent("""
                 import pickle
                 import _xxsubinterpreters as interpreters
                 """),
             shared={},
         )
示例#3
0
def python_entry_point(source_code: str):
    inperpreter = code.InteractiveInterpreter()
    inperpreter.runsource(source_code)
    inperpreter.runcode(code.compile_command(source_code))

    console = code.InteractiveConsole()
    console.push(source_code)

    test.support.run_in_subinterp(source_code)
    _testcapi.run_in_subinterp(source_code)
    _xxsubinterpreters.run_string(source_code)
def run(host: str, port: int, results: Queue):
    # Create a communication channel
    channel_id = subinterpreters.channel_create()
    interpid = subinterpreters.create()
    subinterpreters.run_string(interpid,
                               tw.dedent("""
    import socket; import _xxsubinterpreters as subinterpreters
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(timeout)
    result = sock.connect_ex((host, port))
    subinterpreters.channel_send(channel_id, result)
    sock.close()
    """),
                               shared=dict(channel_id=channel_id,
                                           host=host,
                                           port=port,
                                           timeout=timeout))
    output = subinterpreters.channel_recv(channel_id)
    subinterpreters.channel_release(channel_id)
    if output == 0:
        results.put(port)
示例#5
0
 def run_payload(payload: str) -> None:
     _xxsubinterpreters.run_string(_xxsubinterpreters.create(), payload)