Skip to main content
Join the official Python Developers Survey 2018 and win valuable prizes: Start the survey!

通过装饰器方式实现生产者消费者模式。可以作用于函数、类的方法上,使其变为异步调用,同时,转变为逐次调用,批量执行。方便将零碎的调用转变为批量形势进行统一执行。

Project description

ProducerConsumerPattern

Build Status

通过装饰器方式实现生产者消费者模式。 可以作用于函数、类的方法上,使其变为异步调用,同时,转变为逐次调用,批量执行。方便将零碎的调用转变为批量形势进行统一执行。

Github 地址:ProducerConsumerPattern

安装

pip install prodconpattern

示例代码

函数调用:逐次调用 -> 异步逐次调用

from prodconpattern import ProducerConsumer

@ProducerConsumer(threshold=3)
def method_single_invoke(n):
    """函数调用:逐次调用 -> 异步逐次调用"""
    print('method_single_invoke ->', n)

import time

# 函数调用:逐次调用 -> 异步逐次调用
for n in range(10):
    print("call -> method_single_invoke(%d)" % n)
    method_single_invoke(n)

time.sleep(6)

函数调用:逐次调用 -> 异步批量调用

from prodconpattern import ProducerConsumer

@ProducerConsumer(threshold=3, pass_arg_list=True)
def method_list_invoke(n):
    """函数调用:逐次调用 -> 异步批量调用"""
    print('method_list_invoke ->', n)

import time

# 函数调用:逐次调用 -> 异步批量调用
for n in range(10):
    # time.sleep(1)
    print("call -> method_list_invoke(%d)" % n)
    method_list_invoke(n)

time.sleep(6)

对象方法调用:逐次调用 -> 异步批量调用

from prodconpattern import ProducerConsumer

class AClass:

    @ProducerConsumer(threshold=3, pass_arg_list=True, is_class_method=True)
    def class_method_list_invoke(self, n):
        """对象方法调用:逐次调用 -> 异步批量调用"""
        print(self.__class__.__name__, "print_method", n)

import time

# 对象方法调用:逐次调用 -> 异步批量调用
aaa = AClass()
for n in range(10):
    print("call -> class_method_list_invoke(%d)" % n)
    aaa.class_method_list_invoke(n)

time.sleep(6)

更多例子参考 example.py 文件

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
prodconpattern-0.2.0-py3-none-any.whl (5.0 kB) Copy SHA256 hash SHA256 Wheel py3 Jun 16, 2018
prodconpattern-0.2.0.tar.gz (4.0 kB) Copy SHA256 hash SHA256 Source None Jun 16, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page