Skip to main content

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

Project description

ProducerConsumerPattern

Build Status

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

Github 地址:ProducerConsumerPattern

示例代码

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

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.

Source Distribution

prodconpattern-0.1.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prodconpattern-0.1.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file prodconpattern-0.1.0.tar.gz.

File metadata

File hashes

Hashes for prodconpattern-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2e4d093a8cb840064a447f862724f5fe7af804de321851206a06c7274293a7a7
MD5 b823b9aedd4dbfa559370de63c26fab7
BLAKE2b-256 deb24ab16918eb38cf4d6caa9aa8627fcb25912da8d93ea2fa07a0c69ca8f64c

See more details on using hashes here.

File details

Details for the file prodconpattern-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for prodconpattern-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 577de412515d3ff7427aee55d128d480b41a3003f53f669a4c6af013de3fe197
MD5 0f92dd43a9373108fc1f84e607b08ad3
BLAKE2b-256 bdbbf4098c2dd03a6d2ace8120760de4cde76efded59971455b73a7fa3ec0bf6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page