Skip to main content

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

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.

Source Distribution

prodconpattern-0.2.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

prodconpattern-0.2.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: prodconpattern-0.2.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.13.0 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.5.5

File hashes

Hashes for prodconpattern-0.2.1.tar.gz
Algorithm Hash digest
SHA256 0422299795b0bd3377c75fd362fe5616837fc0cd85857444c8955c734386ef1b
MD5 4ea5a8f96b99e626822afdb0e733cf68
BLAKE2b-256 e7c7f312cde19fbb505e9a8087d8f340b86a874946a2de9b8de406257d149816

See more details on using hashes here.

File details

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

File metadata

  • Download URL: prodconpattern-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.13.0 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.5.5

File hashes

Hashes for prodconpattern-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 21fa2923b29decc739de23f3e4319ad6e7d2345e9eb6449beb7049bf66f7cc09
MD5 66f6898fb1f49fb65347e62ec7b993ce
BLAKE2b-256 2adb332c2b9c841f38913f784ad651b7c1216c32326267bb6813f2554bf5ef9b

See more details on using hashes here.

Supported by

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