コード例 #1
0
ファイル: test_public.py プロジェクト: clchiou/garage
    def test_contexts(self):
        def test_with_kernel():
            self.assertIsNotNone(kernels.get_kernel())

        self.assertIsNone(kernels.get_kernel())
        kernels.call_with_kernel(test_with_kernel)
        self.assertIsNone(kernels.get_kernel())
コード例 #2
0
ファイル: monitors.py プロジェクト: clchiou/garage
async def monitor(
    period,
    executor_queue_threshold,
    num_tasks_threshold,
    executor_queue,
):
    while True:
        queue_length = len(executor_queue)
        stats = kernels.get_kernel().get_stats()
        if (
            queue_length >= executor_queue_threshold
            or stats.num_tasks >= num_tasks_threshold
        ):
            LOG.info(
                'executor_queue_length=%d kernel_stats=%r',
                queue_length,
                stats,
            )
        await timers.sleep(period)
コード例 #3
0
ファイル: test_queues.py プロジェクト: clchiou/garage
 def test_queue_without_kernel(self):
     self.assertIsNone(kernels.get_kernel())
     checks = [
         (queues.Queue, [1, 2, 3], [1, 2, 3]),
         (queues.PriorityQueue, [1, 3, 2], [1, 2, 3]),
         (queues.LifoQueue, [1, 2, 3], [3, 2, 1]),
     ]
     for cls, test_input, expect in checks:
         with self.subTest(check=cls.__name__):
             queue = cls()
             self.assertFalse(queue)
             for item in test_input:
                 queue.put_nonblocking(item)
             self.assertTrue(queue)
             self.assertEqual(len(queue), len(test_input))
             self.assertFalse(queue.is_full())
             self.assertFalse(queue.is_closed())
             actual = []
             while queue:
                 actual.append(queue.get_nonblocking())
             self.assertEqual(actual, expect)
コード例 #4
0
    async def run(self, handle):

        event = locks.Event()

        kernel = ASSERT.not_none(kernels.get_kernel())
        callback = _nng.nng_aio_callback(
            lambda _: kernel.post_callback(event.set))

        aio_p = _nng.nng_aio_p()
        errors.check(_nng.F.nng_aio_alloc(ctypes.byref(aio_p), callback, None))
        try:

            # Strangely, the default is not ``NNG_DURATION_DEFAULT`` but
            # ``NNG_DURATION_INFINITE``; let's make default the default.
            _nng.F.nng_aio_set_timeout(aio_p, _nng.NNG_DURATION_DEFAULT)

            self.transceive(handle, aio_p)

            try:
                await event.wait()
            except BaseException:
                _nng.F.nng_aio_cancel(aio_p)
                raise

            errors.check(_nng.F.nng_aio_result(aio_p))

            return self.make_result(aio_p)

        finally:

            # Call ``nng_aio_wait`` to ensure that AIO is completed and
            # we may safely read its result or free it (in case we are
            # here due to an exception).
            _nng.F.nng_aio_wait(aio_p)

            self.cleanup(aio_p)

            _nng.F.nng_aio_free(aio_p)
コード例 #5
0
ファイル: test_public.py プロジェクト: clchiou/garage
 def test_with_kernel():
     self.assertIsNotNone(kernels.get_kernel())
コード例 #6
0
ファイル: test_public.py プロジェクト: clchiou/garage
def inner(ks, steps):
    k = kernels.get_kernel()
    ks.append(k)
    steps.append(2)
コード例 #7
0
ファイル: test_public.py プロジェクト: clchiou/garage
def outer(ks, steps):
    k = kernels.get_kernel()
    ks.append(k)
    steps.append(1)
    inner(ks, steps)
    steps.append(3)
コード例 #8
0
ファイル: join.py プロジェクト: clchiou/garage
async def compute(x):
    duration = random.uniform(0, 4)
    print('spend %.3f seconds computing' % duration)
    await timers.sleep(duration)
    return x * x


async def accumulate(ts):
    total = 0
    for task in ts:
        total += await task.get_result()
    return total


@kernels.with_kernel
def main(_):
    start = time.perf_counter()
    ts = [tasks.spawn(compute(i + 1)) for i in range(10)]
    total = kernels.run(accumulate(ts))
    elapsed = time.perf_counter() - start
    print('result: %d' % total)
    print('total elapsed time: %.3f seconds' % elapsed)
    return 0


if __name__ == '__main__':
    print('before main: kernel=%s' % kernels.get_kernel())
    status = main(sys.argv)
    print('after main: kernel=%s' % kernels.get_kernel())
    sys.exit(status)
コード例 #9
0
async def compute(x):
    duration = random.uniform(0, 4)
    print('spend %.3f seconds computing' % duration)
    await timers.sleep(duration)
    return x * x


async def accumulate(ts):
    total = 0
    for task in ts:
        total += await task.get_result()
    return total


def main(_):
    start = time.perf_counter()
    ts = [tasks.spawn(compute(i + 1)) for i in range(10)]
    total = kernels.run(accumulate(ts))
    elapsed = time.perf_counter() - start
    print('result: %d' % total)
    print('total elapsed time: %.3f seconds' % elapsed)
    return 0


if __name__ == '__main__':
    print('before call_with_kernel: kernel=%s' % kernels.get_kernel())
    status = kernels.call_with_kernel(main, sys.argv)
    print('after call_with_kernel: kernel=%s' % kernels.get_kernel())
    sys.exit(status)