Skip to main content

1. pip install tps_threadpool_executor

这个线程池和一般线程池不同,是自动控频的,能够将任意耗时大小的函数控制成指定的运行频率。

此线程池入参不是设置并发大小,而是设置tps大小。

实现代码

import time
from queue import Queue
import threading
from threadpool_executor_shrink_able.sharp_threadpoolexecutor import ThreadPoolExecutorShrinkAble

"""
这个线程池和一般线程池不同,是自动控频的,能够将任意函数控制成指定的运行频率。
此线程池入参不是设置并发大小,而是设置tps大小。
"""

class TpsThreadpoolExecutor:
    def __init__(self, tps=0):
        """
        :param tps:   指定线程池每秒运行多少次函数,为0这不限制运行次数
        """
        self.tps = tps
        self.time_interval = 1 / tps if tps != 0 else 0
        self.pool = ThreadPoolExecutorShrinkAble(500)  # 这是使用的智能线程池,所以可以写很大的数字,具体见另一个包的解释。
        self.queue = Queue(500)
        threading.Thread(target=self._put_task_to_pool_queue).start()

    def submit(self, func, *args, **kwargs):
        self.queue.put((func, args, kwargs))

    def _put_task_to_pool_queue(self):
        while True:
            time.sleep(self.time_interval)
            task = self.queue.get()
            self.pool.submit(task[0], *task[1], **task[2])

    def shutdown(self, wait=True):
        self.pool.shutdown(wait=wait)


if __name__ == '__main__':
    def f1(x):
        time.sleep(0.1)
        print(x)


    def f2(x):
        time.sleep(3)
        print(x)


    tps_pool = TpsThreadpoolExecutor(tps=7)
    for i in range(1000):
        tps_pool.submit(f1, i)
        tps_pool.submit(f1, i * 10)

Metadata

Release files for tps-threadpool-executor 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tps-threadpool-executor 0.2
File Size Uploaded
tps_threadpool_executor-0.2.tar.gz 2.7 kB Details

Release files / tps_threadpool_executor-0.2.tar.gz

Download URL tps_threadpool_executor-0.2.tar.gz
Size 2.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ea70c2cf416330d09ce86a484295f5a4b0557c0dc64ae7b8c6586731b0781fe7
BLAKE2b-256 checksum
How to use checksums
bfc12b5e929a2249b23e7efcb6da9eb3b40a642d0fd9e42927af826ee21fd30a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.6

Release history Release notifications | RSS feed

1.4

1 release file

1.3

1 release file

1.2

1 release file

1.1

1 release file

1.0

1 release file

0.9

1 release file

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

This release

0.2 This release

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page