コード例 #1
0
ファイル: process_launch.py プロジェクト: pombredanne/plumpy
def main():
    with kiwipy.connect(
            'amqp://127.0.0.1') as communicator, tempfile.TemporaryDirectory(
            ) as tmpdir:
        loop = asyncio.get_event_loop()
        persister = plumpy.PicklePersister(tmpdir)
        task_receiver = plumpy.ProcessLauncher(loop=loop, persister=persister)

        def callback(*args, **kwargs):
            fut = plumpy.create_task(functools.partial(task_receiver, *args,
                                                       **kwargs),
                                     loop=loop)
            return fut

        communicator.add_task_subscriber(callback)

        process_controller = plumpy.RemoteProcessThreadController(communicator)

        future = process_controller.launch_process(DummyProcessWithOutput)

        async def task():
            result = await asyncio.wrap_future(future.result())
            print(result)

        loop.run_until_complete(task())
コード例 #2
0
import kiwipy
import sys

message = ' '.join(sys.argv[1:]) or 'Hello World!'

with kiwipy.connect('amqp://localhost') as comm:
    queue = comm.task_queue('task_queue')  # Durable by default
    queue.task_send(message)
    print(f' [x] Sent {message!r}')
コード例 #3
0
import kiwipy


def fib(_comm, num):
    if num == 0:
        return 0
    if num == 1:
        return 1
    return fib(_comm, num - 1) + fib(_comm, num - 2)


def fac(_comm, num):
    result = 1
    if num > 1:
        result = num * fac(_comm, num - 1)
    return result


if __name__ == '__main__':
    try:
        with kiwipy.connect('amqp://127.0.0.1') as comm:
            # Register an RPC subscriber with the name 'fib'
            comm.add_rpc_subscriber(fib, 'fib')
            # Register an RPC subscriber with the name 'fac'
            comm.add_rpc_subscriber(fac, 'fac')
            # Now wait indefinitely for fibonacci calls
            threading.Event().wait()
    except KeyboardInterrupt:
        pass
コード例 #4
0
def comm(connection_params):  # pylint: disable=redefined-outer-name
    encoder = functools.partial(msgpack.packb, use_bin_type=True)
    decoder = functools.partial(msgpack.unpackb, raw=False)

    with kiwipy.connect(connection_params['url'], encoder=encoder, decoder=decoder) as communicator:
        yield communicator
コード例 #5
0
ファイル: rmq_worker.py プロジェクト: unkcpz/kiwipy
# -*- coding: utf-8 -*-
import time
import threading

import kiwipy

print(' [*] Waiting for messages. To exit press CTRL+C')


def callback(_comm, task):
    print(f' [x] Received {task!r}')
    time.sleep(task.count('.'))
    print(' [x] Done')
    return task


with kiwipy.connect('amqp://127.0.0.1/') as communicator:
    communicator.add_task_subscriber(callback)
    try:
        threading.Event().wait()
    except KeyboardInterrupt:
        pass