Пример #1
0
    if random.randint(4, 6) == 5:
        raise RandomError('演示随机出错')
    logger.info(f'计算 {a} + {b} 得到的结果是  {a + b}')
    return a + b


def sub(x, y):
    logger.info(f'消费此消息 {x} - {y} 中。。。。。')
    time.sleep(0.4)  # 模拟做某事需要阻塞10秒种,必须用并发绕过此阻塞。
    if random.randint(1, 10) == 4:
        raise ValueError('4444')
    logger.info(f'计算 {x} - {y} 得到的结果是  {x - y}')


# 把消费的函数名传给consuming_function,就这么简单。
consumer_add = get_consumer('queue_test56', consuming_function=add, threads_num=500, max_retry_times=2,
                            msg_schedule_time_intercal=0.0007, log_level=10, logger_prefix='zz平台消费',
                            function_timeout=0, is_print_detail_exception=False,
                            msg_expire_senconds=3600,
                            function_result_status_persistance_conf= FunctionResultStatusPersistanceConfig(True, True, 7 * 24 * 3600),
                            broker_kind=0, concurrent_mode=1,)  # 通过设置broker_kind,一键切换中间件为rabbitmq或redis等9种中间件或包。

consumer_sub = get_consumer('queue_test57', consuming_function=sub, threads_num=50, msg_schedule_time_intercal=0.2, log_level=10, logger_prefix='xxxxx平台消费',
                            function_timeout=80, is_print_detail_exception=True,
                            broker_kind=2, concurrent_mode=1)  # 通过设置

if __name__ == '__main__':
    ConsumersManager.show_all_consumer_info()
    consumer_add.start_consuming_message()
    # consumer_sub.start_consuming_message()
Пример #2
0
def run_many_consumer_by_init_params(consumer_init_params_list: List[dict]):
    for consumer_init_params in consumer_init_params_list:
        get_consumer(**consumer_init_params).start_consuming_message()
    ConsumersManager.join_all_consumer_shedual_task_thread()