dask/distributed

AttributeError: module 'asyncio' has no attribute 'get_running_loop'

Closed this issue · 5 comments

In Jupyter Lab, If I run:

import distributed

causes AttributeError: module 'asyncio' has no attribute 'get_running_loop' error.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-432545581521> in <module>()
----> 1 import distributed

~/anaconda3/lib/python3.6/site-packages/distributed/__init__.py in <module>()
      1 from . import config
      2 from dask.config import config
----> 3 from .actor import Actor, ActorFuture
      4 from .core import connect, rpc
      5 from .deploy import LocalCluster, Adaptive, SpecCluster, SSHCluster

~/anaconda3/lib/python3.6/site-packages/distributed/actor.py in <module>()
      5 from queue import Queue
      6 
----> 7 from .client import Future, default_client
      8 from .protocol import to_serialize
      9 from .utils import sync

~/anaconda3/lib/python3.6/site-packages/distributed/client.py in <module>()
     47 from asyncio import iscoroutine
     48 
---> 49 from .batched import BatchedSend
     50 from .utils_comm import (
     51     WrappedKey,

~/anaconda3/lib/python3.6/site-packages/distributed/batched.py in <module>()
      6 from tornado.ioloop import IOLoop
      7 
----> 8 from .core import CommClosedError
      9 from .utils import parse_timedelta
     10 

~/anaconda3/lib/python3.6/site-packages/distributed/core.py in <module>()
     16 from tornado.locks import Event
     17 
---> 18 from .comm import (
     19     connect,
     20     listen,

~/anaconda3/lib/python3.6/site-packages/distributed/comm/__init__.py in <module>()
----> 1 from .addressing import (
      2     parse_address,
      3     unparse_address,
      4     normalize_address,
      5     parse_host_port,

~/anaconda3/lib/python3.6/site-packages/distributed/comm/addressing.py in <module>()
      3 
      4 from . import registry
----> 5 from ..utils import get_ip_interface
      6 
      7 

~/anaconda3/lib/python3.6/site-packages/distributed/utils.py in <module>()
   1205         if is_kernel():
   1206             try:
-> 1207                 asyncio.get_running_loop()
   1208             except RuntimeError:
   1209                 is_kernel_and_no_running_loop = True

AttributeError: module 'asyncio' has no attribute 'get_running_loop'

My Environment:

  • OS : Ubuntu 16.04.6 LTS
  • Python : 3.6.6
  • distributed : 2.9.2
  • tornado: 5.0.2

Strangely, command line python interpreter does not raise the error.
distributed 2.9.1 worked fine.

Thanks for the report. Seems that get_running_loop was added in 3.7: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_running_loop

That line is only executed in jupyter notebook / lab, which explains why it doesn't happen in the python interpreter (and why our CI didn't catch it, I think).

xref #3336. @potpath do you know of a way to do that check that's compatible with Python 3.6?

Thank you @haje01 for reporting this!

Although it's not a great solution, we could use the private asyncio._get_running_loop() method

https://github.com/python/cpython/blob/99eb70a9eb9493602ff6ad8bb92df4318cf05a3e/Lib/asyncio/events.py#L681-L690

as a short term fix.

Another option is to revert #3336

cc @jcrist who has asyncio experience

I'll plan to push out a 2.9.3 release of distributed once this issue is resolved

Yeah, I've been using this compat in other projects:

import sys

if sys.version_info[:2] >= (3, 7):
    from asyncio import get_running_loop
else:
    from asyncio import _get_running_loop as get_running_loop

Great, I'll open a PR adding that