1
1
"""Subinterpreters High Level Module."""
2
2
3
3
import _xxsubinterpreters as _interpreters
4
+ # aliases:
5
+ from _xxsubinterpreters import (
6
+ ChannelError , ChannelNotFoundError ,
7
+ ChannelEmptyError , ChannelNotEmptyError , NotReceivedError ,
8
+ is_shareable ,
9
+ )
4
10
5
11
__all__ = [
6
12
'Interpreter' , 'get_current' , 'get_main' , 'create' , 'list_all' ,
7
13
'SendChannel' , 'RecvChannel' ,
8
14
'create_channel' , 'list_all_channels' , 'is_shareable' ,
15
+ 'ChannelError' , 'ChannelNotFoundError' ,
16
+ 'ChannelEmptyError' , 'ChannelNotEmptyError' ,
17
+ 'NotReceivedError' ,
9
18
]
10
19
11
20
12
21
def create (* , isolated = True ):
13
22
"""
14
23
Initialize a new (idle) Python interpreter.
15
-
16
24
"""
17
25
id = _interpreters .create (isolated = isolated )
18
26
return Interpreter (id , isolated = isolated )
@@ -80,19 +88,9 @@ def run(self, src_str, /, *, channels=None):
80
88
Run the given source code in the interpreter.
81
89
This blocks the current Python thread until done.
82
90
"""
83
- try :
84
- _interpreters .run_string (self ._id , src_str )
85
- except _interpreters .RunFailedError as err :
86
- raise
91
+ _interpreters .run_string (self ._id , src_str )
87
92
88
93
89
- def is_shareable (obj ):
90
- """
91
- Return `True` if the object's data can be
92
- shared between interpreters and `False` otherwise.
93
- """
94
- return _interpreters .is_shareable (obj )
95
-
96
94
def create_channel ():
97
95
"""
98
96
Create a new channel for passing data between
@@ -119,8 +117,8 @@ class RecvChannel:
119
117
def __init__ (self , id ):
120
118
self ._id = id
121
119
122
- def recv (self , * , _delay = 10 / 1000 ): # seconds
123
- """ channel_recv() -> obj
120
+ def recv (self , * , _delay = 10 / 1000 ): # 10 milliseconds
121
+ """
124
122
Get the next object from the channel,
125
123
and wait if none have been sent.
126
124
Associate the interpreter with the channel.
@@ -133,19 +131,22 @@ def recv(self, *, _delay=10 / 1000): # seconds
133
131
obj = _interpreters .channel_recv (self ._id , sentinel )
134
132
return obj
135
133
136
- _NOT_SET = object ()
137
-
138
134
def recv_nowait (self , default = None ):
139
135
"""
140
136
Like recv(), but return the default
141
137
instead of waiting.
142
- """
143
138
139
+ This function is blocked by a missing low-level
140
+ implementation of channel_recv_wait().
141
+ """
144
142
if default is None :
143
+ default = _NOT_SET
144
+ if default is _NOT_SET :
145
145
return _interpreters .channel_recv (self ._id )
146
146
else :
147
147
return _interpreters .channel_recv (self ._id , default )
148
148
149
+ _NOT_SET = object ()
149
150
150
151
class SendChannel :
151
152
"""
@@ -169,6 +170,9 @@ def send(self, obj):
169
170
def send_nowait (self , obj ):
170
171
"""
171
172
Like send(), but return False if not received.
173
+
174
+ This function is blocked by a missing low-level
175
+ implementation of channel_send_wait().
172
176
"""
173
177
174
178
_interpreters .channel_send (self ._id , obj )
0 commit comments