This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _xxsubinterpreters as interpreters | |
import threading | |
import textwrap as tw | |
import marshal | |
# Create a sub-interpreter | |
interpid = interpreters.create() | |
# If you had a function that generated some data | |
arry = list(range(0,100)) | |
# Create a channel | |
channel_id = interpreters.channel_create() | |
# Pre-populate the interpreter with a module | |
interpreters.run_string(interpid, "import marshal; import _xxsubinterpreters as interpreters") | |
# Define a | |
def run(interpid, channel_id): | |
interpreters.run_string(interpid, | |
tw.dedent(""" | |
arry_raw = interpreters.channel_recv(channel_id) | |
arry = marshal.loads(arry_raw) | |
result = [1,2,3,4,5] # where you would do some calculating | |
result_raw = marshal.dumps(result) | |
interpreters.channel_send(channel_id, result_raw) | |
"""), | |
shared=dict( | |
channel_id=channel_id | |
), | |
) | |
inp = marshal.dumps(arry) | |
interpreters.channel_send(channel_id, inp) | |
# Run inside a thread | |
t = threading.Thread(target=run, args=(interpid, channel_id)) | |
t.start() | |
# Sub interpreter will process. Feel free to do anything else now. | |
output = interpreters.channel_recv(channel_id) | |
interpreters.channel_release(channel_id) | |
output_arry = marshal.loads(output) | |
print(output_arry) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment