def yields(value):
   """
   Return True iff the value yields.

   See: http://stackoverflow.com/questions/20730248/maybedeferred-analog-with-asyncio
   """
   return isinstance(value, Future) or iscoroutine(value)
def yields(value):
    """
   Return True iff the value yields.

   See: http://stackoverflow.com/questions/20730248/maybedeferred-analog-with-asyncio
   """
    return isinstance(value, Future) or iscoroutine(value)
Exemple #3
0
    def test_coroutine_non_gen_function(self):
        @tasks.coroutine
        def func():
            return 'test'

        self.assertTrue(tasks.iscoroutinefunction(func))

        coro = func()
        self.assertTrue(tasks.iscoroutine(coro))

        res = self.loop.run_until_complete(coro)
        self.assertEqual(res, 'test')
Exemple #4
0
    def test_coroutine_non_gen_function(self):
        @tasks.coroutine
        def func():
            return 'test'

        self.assertTrue(tasks.iscoroutinefunction(func))

        coro = func()
        self.assertTrue(tasks.iscoroutine(coro))

        res = self.loop.run_until_complete(coro)
        self.assertEqual(res, 'test')
Exemple #5
0
 def _as_future(fun, *args, **kwargs):
    try:
       res = fun(*args, **kwargs)
    except Exception as e:
       f = Future()
       f.set_exception(e)
       return f
    else:
       if isinstance(res, Future):
          return res
       elif iscoroutine(res):
          return asyncio.Task(res)
       else:
          f = Future()
          f.set_result(res)
          return f
Exemple #6
0
 def _as_future(fun, *args, **kwargs):
    try:
       res = fun(*args, **kwargs)
    except Exception as e:
       f = Future()
       f.set_exception(e)
       return f
    else:
       if isinstance(res, Future):
          return res
       elif iscoroutine(res):
          return asyncio.Task(res)
       else:
          f = Future()
          f.set_result(res)
          return f
Exemple #7
0
def maybe_coroutine(callable, *args, **kwargs):
    """Invoke a function that may or may not be a coroutine.

    This is analogous to `~twisted.internet.defer.maybeDeferred`, but for
    `asyncio`.

    """

    value = callable(*args, **kwargs)

    if iscoroutine(value):
        return value

    def coro():
        yield
        return value
    return coro()
Exemple #8
0
def maybe_coroutine(callable, *args, **kwargs):
    """Invoke a function that may or may not be a coroutine.

    This is analogous to `~twisted.internet.defer.maybeDeferred`, but for
    `asyncio`.

    """

    value = callable(*args, **kwargs)

    if iscoroutine(value):
        return value

    def coro():
        yield
        return value

    return coro()
Exemple #9
0
    def updater(self):
        while True:
            # if the update method is proven to be asynchronous, e.g. it's
            # running a subprocess, or doing some network activity, then
            # we'll get the result by yielding from it
            future_or_result = self.update()

            if iscoroutine(future_or_result):
                result = yield from future_or_result
            else:
                result = future_or_result
            self.cachestr = result

            # tell the printing coroutine that it's time to update
            yield from UPDATE_QUEUE.put(True)

            # put this coroutine to sleep until it's time to update again
            # sleep for a minimum of one second so
            # we don't update as fast as possible.
            yield from asyncio.sleep(self.cachetime or 1)