2121except ImportError :
2222 PollIOLoop = None # dropped in tornado 6.0
2323
24+ try :
25+ from distributed .client import default_client as _dask_default_client
26+ except ImportError : # pragma: no cover
27+ _dask_default_client = None
28+
2429from collections .abc import Iterable
2530
2631from threading import get_ident as get_thread_identity
@@ -42,6 +47,15 @@ def get_io_loop(asynchronous=None):
4247 if asynchronous :
4348 return IOLoop .current ()
4449
50+ if _dask_default_client is not None :
51+ try :
52+ client = _dask_default_client ()
53+ except ValueError :
54+ # No dask client found; continue
55+ pass
56+ else :
57+ return client .loop
58+
4559 if not _io_loops :
4660 loop = IOLoop ()
4761 thread = threading .Thread (target = loop .start )
@@ -163,6 +177,7 @@ class Stream(object):
163177
164178 def __init__ (self , upstream = None , upstreams = None , stream_name = None ,
165179 loop = None , asynchronous = None , ensure_io_loop = False ):
180+ self .name = stream_name
166181 self .downstreams = OrderedWeakrefSet ()
167182 if upstreams is not None :
168183 self .upstreams = list (upstreams )
@@ -182,8 +197,6 @@ def __init__(self, upstream=None, upstreams=None, stream_name=None,
182197 if upstream :
183198 upstream .downstreams .add (self )
184199
185- self .name = stream_name
186-
187200 def _set_loop (self , loop ):
188201 self .loop = None
189202 if loop is not None :
@@ -343,8 +356,6 @@ def __str__(self):
343356 s = str (at )
344357 elif hasattr (at , '__name__' ):
345358 s = getattr (self , m ).__name__
346- elif hasattr (at .__class__ , '__name__' ):
347- s = getattr (self , m ).__class__ .__name__
348359 else :
349360 s = None
350361 if s :
@@ -994,7 +1005,8 @@ def __init__(self, upstream, n, timeout=None, key=None, **kwargs):
9941005 self ._buffer = defaultdict (lambda : [])
9951006 self ._metadata_buffer = defaultdict (lambda : [])
9961007 self ._callbacks = {}
997- Stream .__init__ (self , upstream , ensure_io_loop = True , ** kwargs )
1008+ kwargs ["ensure_io_loop" ] = True
1009+ Stream .__init__ (self , upstream , ** kwargs )
9981010
9991011 def _get_key (self , x ):
10001012 if self ._key is None :
@@ -1206,7 +1218,8 @@ def __init__(self, upstream, interval, **kwargs):
12061218 self .metadata_buffer = []
12071219 self .last = gen .moment
12081220
1209- Stream .__init__ (self , upstream , ensure_io_loop = True , ** kwargs )
1221+ kwargs ["ensure_io_loop" ] = True
1222+ Stream .__init__ (self , upstream , ** kwargs )
12101223
12111224 self .loop .add_callback (self .cb )
12121225
@@ -1308,7 +1321,8 @@ def __init__(
13081321 self ._buffer = {}
13091322 self ._metadata_buffer = {}
13101323 self .last = gen .moment
1311- Stream .__init__ (self , upstream , ensure_io_loop = True , ** kwargs )
1324+ kwargs ["ensure_io_loop" ] = True
1325+ Stream .__init__ (self , upstream , ** kwargs )
13121326 self .loop .add_callback (self .cb )
13131327
13141328 def _get_key (self , x ):
@@ -1355,7 +1369,8 @@ def __init__(self, upstream, interval, **kwargs):
13551369 self .interval = convert_interval (interval )
13561370 self .queue = Queue ()
13571371
1358- Stream .__init__ (self , upstream , ensure_io_loop = True , ** kwargs )
1372+ kwargs ["ensure_io_loop" ] = True
1373+ Stream .__init__ (self , upstream ,** kwargs )
13591374
13601375 self .loop .add_callback (self .cb )
13611376
@@ -1393,7 +1408,8 @@ def __init__(self, upstream, interval, **kwargs):
13931408 self .interval = convert_interval (interval )
13941409 self .next = 0
13951410
1396- Stream .__init__ (self , upstream , ensure_io_loop = True , ** kwargs )
1411+ kwargs ["ensure_io_loop" ] = True
1412+ Stream .__init__ (self , upstream , ** kwargs )
13971413
13981414 @gen .coroutine
13991415 def update (self , x , who = None , metadata = None ):
@@ -1418,7 +1434,8 @@ class buffer(Stream):
14181434 def __init__ (self , upstream , n , ** kwargs ):
14191435 self .queue = Queue (maxsize = n )
14201436
1421- Stream .__init__ (self , upstream , ensure_io_loop = True , ** kwargs )
1437+ kwargs ["ensure_io_loop" ] = True
1438+ Stream .__init__ (self , upstream , ** kwargs )
14221439
14231440 self .loop .add_callback (self .cb )
14241441
@@ -1862,7 +1879,8 @@ def __init__(self, upstream, **kwargs):
18621879 self .next = []
18631880 self .next_metadata = None
18641881
1865- Stream .__init__ (self , upstream , ensure_io_loop = True , ** kwargs )
1882+ kwargs ["ensure_io_loop" ] = True
1883+ Stream .__init__ (self , upstream , ** kwargs )
18661884
18671885 self .loop .add_callback (self .cb )
18681886
@@ -1918,7 +1936,8 @@ def __init__(self, upstream, topic, producer_config, **kwargs):
19181936 self .topic = topic
19191937 self .producer = ck .Producer (producer_config )
19201938
1921- Stream .__init__ (self , upstream , ensure_io_loop = True , ** kwargs )
1939+ kwargs ["ensure_io_loop" ] = True
1940+ Stream .__init__ (self , upstream , ** kwargs )
19221941 self .stopped = False
19231942 self .polltime = 0.2
19241943 self .loop .add_callback (self .poll )
0 commit comments