@@ -54,6 +54,16 @@ class _ShutdownStatus(Enum):
54
54
F = t .TypeVar ('F' , bound = t .Callable [..., t .Any ])
55
55
56
56
57
+ def _get_future () -> t .Union [Future , CFuture ]:
58
+ """Get an appropriate Future object"""
59
+ try :
60
+ asyncio .get_running_loop ()
61
+ return Future ()
62
+ except RuntimeError :
63
+ # No event loop running, use concurrent future
64
+ return CFuture ()
65
+
66
+
57
67
def in_pending_state (method : F ) -> F :
58
68
"""Sets the kernel to a pending state by
59
69
creating a fresh Future for the KernelManager's `ready`
@@ -64,12 +74,8 @@ def in_pending_state(method: F) -> F:
64
74
@functools .wraps (method )
65
75
async def wrapper (self , * args , ** kwargs ):
66
76
# Create a future for the decorated method
67
- if self ._attempted_start :
68
- try :
69
- self ._ready = Future ()
70
- except RuntimeError :
71
- # No event loop running, use concurrent future
72
- self ._ready = CFuture ()
77
+ if self ._attempted_start or not self ._ready :
78
+ self ._ready = _get_future ()
73
79
try :
74
80
# call wrapped method, await, and set the result or exception.
75
81
out = await method (self , * args , ** kwargs )
@@ -91,19 +97,13 @@ class KernelManager(ConnectionFileMixin):
91
97
This version starts kernels with Popen.
92
98
"""
93
99
94
- _ready : t .Union [Future , CFuture ]
100
+ _ready : t .Optional [ t . Union [Future , CFuture ] ]
95
101
96
102
def __init__ (self , * args , ** kwargs ):
97
103
super ().__init__ (** kwargs )
98
104
self ._shutdown_status = _ShutdownStatus .Unset
99
105
self ._attempted_start = False
100
- # Create a place holder future.
101
- try :
102
- asyncio .get_running_loop ()
103
- self ._ready = Future ()
104
- except RuntimeError :
105
- # No event loop running, use concurrent future
106
- self ._ready = CFuture ()
106
+ self ._ready = None
107
107
108
108
_created_context : Bool = Bool (False )
109
109
@@ -188,6 +188,8 @@ def _default_cache_ports(self) -> bool:
188
188
@property
189
189
def ready (self ) -> t .Union [CFuture , Future ]:
190
190
"""A future that resolves when the kernel process has started for the first time"""
191
+ if not self ._ready :
192
+ self ._ready = _get_future ()
191
193
return self ._ready
192
194
193
195
@property
0 commit comments