Git Product home page Git Product logo

lxngoddess5321 / producerconsumerpattern Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mmmaaaggg/producerconsumerpattern

0.0 0.0 0.0 10 KB

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

License: MIT License

Python 99.51% Batchfile 0.49%

producerconsumerpattern's Introduction

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 文件

producerconsumerpattern's People

Contributors

mmmaaaggg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.