code: def calculate_backend(name_from_env, backends=None):
      
      if backends is None:
          backends = _default_backends
      if name_from_env:
          if name_from_env not in backends:
              raise RuntimeError(
                  'Invalid backend %r specified. Valid backends are: %s'
                  % (name_from_env, _default_backends.keys()))
          # Allow this to raise, since it was explicitly set from the environment
          # noinspection PyCallingNonCallable
          return backends[name_from_env]()
      try:
          return _calc_default(backends)
      except SystemError:
          pass
      for maker in backends.values():
          # noinspection PyBroadException
          try:
              return maker()
          except Exception:
              pass
      return NullBackend()
docs: Calculates which backend to use with the following algorithm:
  
      - Try to read the GOLESS_BACKEND environment variable.
        Usually 'gevent' or 'stackless'.
        If a value is set but no backend is available or it fails to be created,
        this function will error.
      - Determine the default backend (gevent for PyPy, stackless for Python).
        If no default can be determined or created, continue.
      - Try to create all the runtimes and choose the first one to create
        successfully.
      - If no runtime can be created, return a NullBackend,
        which will error when accessed.
  
      The "default" backend is the less-easy backend for a runtime.
      Since PyPy has stackless by default, gevent is intentional.
      Since Stackless is a separate interpreter for CPython,
      that is more intentional than gevent.
      We feel this is a good default behavior.