Ejemplo n.º 1
0
    def test_all_workers_are_present(self):
        expected = 268
        expected_workers_amount = 5
        expected_workers = {i for i in range(1, expected_workers_amount + 1)}

        produce(expected)

        key = 'test:amount'
        key_workerids = 'test:workerids'
        kwargs = {'key': key, 'r': RedisQueue}
        init_kwargs = {'r': example_config}

        startapp(incr_and_append_worker_id,
                 workers=expected_workers_amount,
                 config=example_config,
                 func_kwargs=kwargs,
                 init_kwargs=init_kwargs)

        result = int(redis_instance.get(key))
        self.assertEqual(result, expected)

        result_workers = redis_instance.lrange(key_workerids, 0, -1)
        result_workers_set = {int(i) for i in sorted(result_workers)}

        self.assertEqual(result_workers_set, expected_workers)
Ejemplo n.º 2
0
    def test_handle_sys(self):
        items = ['1', '2', 'still_worked_on']
        produce_items(items)
        list_key = '{}:{}'.format(example_config['namespace'],
                                  example_config['key'])

        startapp(sysexit, workers=5, config=example_config)

        result = [
            i.decode('utf-8') for i in redis_instance.lrange(list_key, 0, -1)
        ]
        self.assertEqual(result, ['still_worked_on'])
Ejemplo n.º 3
0
    def test_incr_key_equals_produces_multiple_workers(self):
        expected = 123
        produce(expected)

        key = 'test:amount'
        kwargs = {'key': key, 'r': RedisQueue}
        init_kwargs = {'r': example_config}

        startapp(increment_by_one,
                 workers=7,
                 config=example_config,
                 func_kwargs=kwargs,
                 init_kwargs=init_kwargs)

        result = int(redis_instance.get(key))
        self.assertEqual(result, expected)
Ejemplo n.º 4
0
    def test_items_send_are_handled_multiple_worker(self):
        expected = ['1', '2', '3']
        produce_items(expected)

        key = 'test:items'
        result_key = 'test:result'
        kwargs = {'key': key, 'r': RedisQueue, 'test_result_key': result_key}
        init_kwargs = {'r': example_config}

        startapp(append_item,
                 workers=5,
                 config=example_config,
                 func_kwargs=kwargs,
                 init_kwargs=init_kwargs)

        result = [
            i.decode('utf-8')
            for i in redis_instance.lrange(result_key, 0, -1)
        ]
        self.assertEqual(sorted(result), sorted(expected))
Ejemplo n.º 5
0
            self.alive = bool(random.getrandbits(1))
        return self.alive


def produce(items):
    r = RedisQueue(**config)
    for i in range(items):
        r.send(i)


def my_func(item, worker_id, ana, bob, citrus):
    time.sleep(1)
    ana.look_in_the_box()
    bob.look_in_the_box()
    citrus.look_in_the_box()
    if bool(random.getrandbits(1)):
        raise Exception
    print('finished item', locals())


if __name__ == "__main__":
    # produce 100 items
    produce(100)

    names = ['ana', 'bob', 'citrus']
    kwargs = {name: SchrodingerCat for name in names}
    init_kwargs = {name: {'name': name} for name in names}

    # stop with keyboard interrupt
    startapp(my_func, workers=10, config=config, func_kwargs=kwargs, init_kwargs=init_kwargs)
Ejemplo n.º 6
0
import sys
from meesee import startapp

config = {
    "namespace": "removeme",
    "key": "tasks",
    "redis_config": {},
    "maxsize": 100,
    "timeout": 1,
}


def my_func(item, worker_id):
    print('worker: {worker_id} hello, look at me, msg: {item}'.format(
        worker_id=worker_id, item=item))


if __name__ == '__main__':
    workers = int(sys.argv[sys.argv.index('-w') +
                           1]) if '-w' in sys.argv else 10
    startapp(my_func, workers=workers, config=config)
Ejemplo n.º 7
0
config = {
    "namespace": "removeme",
    "key": "tasks",
    "redis_config": {},
    "maxsize": 100,
    "timeout": 1,
}


def func_a(item, worker_id):
    print('func: {}, worker_id: {}, item: {}'.format('func_a', worker_id,
                                                     item))


def func_b(item, worker_id):
    print('func: {}, worker_id: {}, item: {}'.format('func_b', worker_id,
                                                     item))


def func_c(item, worker_id):
    print('func: {}, worker_id: {}, item: {}'.format('func_c', worker_id,
                                                     item))


funcs = [func_a, func_b, func_c]

if __name__ == '__main__':
    workers = int(sys.argv[sys.argv.index('-w') +
                           1]) if '-w' in sys.argv else 10
    startapp(funcs, workers=workers, config=config)
Ejemplo n.º 8
0
config = {
    "namespace": "remove_me",
    "key": "tasks",
    "redis_config": {},
    "maxsize": 100,
    "timeout": 1,
}


def produce(items):
    r = RedisQueue(**config)
    for i in range(items):
        r.send(i)


def my_func(item, worker_id):
    if item is None or item == 'None':
        print('regression: item is None')
    print('got item {}'.format(locals()))


def raise_sys_exit(item, worker_id):
    print('raise sys_exit')
    raise SystemExit


if __name__ == "__main__":
    produce(1)
    startapp(raise_sys_exit, workers=1, config=config)
    startapp(my_func, workers=1, config=config)
Ejemplo n.º 9
0
    "namespace": "removeme",
    "key": "tasks",
    "redis_config": {},
    "maxsize": 100
}


def produce(items):
    r = RedisQueue(**config)
    for i in range(items):
        r.send(i)


def my_func(item, worker_id, name, number):
    print("hello, look at me")
    time.sleep(1)
    print('finished item', locals())


if __name__ == "__main__":
    # produce 10 items
    produce(10)
    # stop with keyboard interrupt
    startapp(my_func,
             workers=10,
             config=config,
             func_kwargs={
                 'name': 'my_name',
                 'number': 9
             })