On 5/23/2020 6:24 PM, redradist@gmail.com wrote:
Hi all,
I am very exciting about the sub-interpreters ... but I do not like some parts ... Consider the following code: ```python import _xxsubinterpreters as interpreters import threading import textwrap as tw import marshal
if __name__ == '__main__': # 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): # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Still is run in thread which is use GIL !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 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) ```
Add some `async` execution of `interpreters.run_string_async` and other async functions !!
Also regarding the threads ... I think it is better to make `threading.Thread` as green `Thread` !!
I know, I know, `Thread` is not possible due to `sync io` ... But we can to change underling `api` from `sync io` to `async io` !! User will notice nothing, but performance will be increased mush more !! Only if your workload is CPU bound. Python optimizes IO bound workload performance by releasing the GIL while doing IO. Green threads generally do not offer this option.
--Edwin