Skip to main content

Decorators for running functions in Thread/ThreadPool/IOLoop

Project description

threaded

https://travis-ci.org/penguinolog/threaded.svg?branch=master https://img.shields.io/appveyor/ci/penguinolog/threaded.svg https://coveralls.io/repos/github/penguinolog/threaded/badge.svg?branch=master Documentation Status https://img.shields.io/pypi/v/threaded.svg https://img.shields.io/pypi/pyversions/threaded.svg https://img.shields.io/pypi/status/threaded.svg https://img.shields.io/github/license/penguinolog/threaded.svg

threaded is a set of decorators, which wrap functions in:

  • concurrent.futures.ThreadPool

  • threading.Thread

  • asyncio.Task in Python 3.

  • gevent.threadpool.ThreadPool if gevent is installed.

Why? Because copy-paste of loop.create_task, threading.Thread and thread_pool.submit is boring, especially if target functions is used by this way only.

Pros:

Python 2.7
Python 3.4
Python 3.5
Python 3.6
PyPy
PyPy3 3.5+
Jyton 2.7

Decorators:

  • ThreadPooled - native concurrent.futures.ThreadPool usage on Python 3 and it’s backport on Python 2.7.

  • threadpooled is alias for ThreadPooled.

  • Threaded - wrap in threading.Thread.

  • threaded is alias for Threaded.

  • AsyncIOTask - wrap in asyncio.Task. Uses the same API, as Python 3 ThreadPooled.

  • asynciotask is alias for AsyncIOTask.

  • GThreadPooled - wrap function in gevent.threadpool.ThreadPool.

  • gthreadpooled is alias for GThreadPooled.

Usage

ThreadPooled

Mostly it is required decorator: submit function to ThreadPoolExecutor on call.

threaded.ThreadPooled.configure(max_workers=3)

Python 2.7 usage:

@threaded.ThreadPooled
def func():
    pass

concurrent.futures.wait([func()])

Python 3.3+ usage:

@threaded.ThreadPooled
def func():
    pass

concurrent.futures.wait([func()])

Python 3.3+ usage with asyncio:

loop = asyncio.get_event_loop()
@threaded.ThreadPooled(loop_getter=loop, loop_getter_need_context=False)
def func():
    pass

loop.run_until_complete(asyncio.wait_for(func(), timeout))

Python 3.3+ usage with asyncio and loop extraction from call arguments:

loop_getter = lambda tgt_loop: tgt_loop
@threaded.ThreadPooled(loop_getter=loop_getter, loop_getter_need_context=True)  # loop_getter_need_context is required
def func(*args, **kwargs):
    pass

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait_for(func(loop), timeout))

During application shutdown, pool can be stopped (while it will be recreated automatically, if some component will request).

threaded.ThreadPooled.shutdown()

Threaded

Classic threading.Thread. Useful for running until close and self-closing threads without return.

Usage example:

@threaded.Threaded
def func(*args, **kwargs):
    pass

thread = func()
thread.start()
thread.join()

Without arguments, thread name will use pattern: 'Threaded: ' + func.__name__

Override name can be don via corresponding argument:

@threaded.Threaded(name='Function in thread')
def func(*args, **kwargs):
    pass

Thread can be daemonized automatically:

@threaded.Threaded(daemon=True)
def func(*args, **kwargs):
    pass

Also, if no any addition manipulations expected before thread start, it can be started automatically before return:

@threaded.Threaded(started=True)
def func(*args, **kwargs):
    pass

AsyncIOTask

Wrap in asyncio.Task.

usage with asyncio:

@threaded.AsyncIOTask
def func():
    pass

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait_for(func(), timeout))

Provide event loop directly:

loop = asyncio.get_event_loop()
@threaded.AsyncIOTask(loop_getter=loop)
def func():
    pass

loop.run_until_complete(asyncio.wait_for(func(), timeout))

Usage with loop extraction from call arguments:

loop_getter = lambda tgt_loop: tgt_loop
@threaded.AsyncIOTask(loop_getter=loop_getter, loop_getter_need_context=True)
def func(*args, **kwargs):
    pass

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait_for(func(loop), timeout))

GThreadPooled

Post function to gevent.threadpool.ThreadPool.

threaded.GThreadPooled.configure(max_workers=3)

Basic usage example:

@threaded.GThreadPooled
def func():
    pass

func().wait()

Testing

The main test mechanism for the package threaded is using tox. Test environments available:

pep8
py27
py34
py35
py36
pypy
pypy3
pylint

CI systems

For code checking several CI systems is used in parallel:

  1. Travis CI: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests. Also it’s publishes coverage on coveralls.

  2. AppVeyor: is used for checking windows compatibility.

  3. coveralls: is used for coverage display.

CD system

Travis CI: is used for package delivery on PyPI.

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

threaded-0.9.1.zip (303.3 kB view details)

Uploaded Source

Built Distributions

threaded-0.9.1-py2-none-any.whl (19.1 kB view details)

Uploaded Python 2

threaded-0.9.1-cp36-none-win_amd64.whl (188.9 kB view details)

Uploaded CPython 3.6 Windows x86-64

threaded-0.9.1-cp36-none-win32.whl (162.1 kB view details)

Uploaded CPython 3.6 Windows x86

threaded-0.9.1-cp36-cp36m-manylinux1_x86_64.whl (708.4 kB view details)

Uploaded CPython 3.6m

threaded-0.9.1-cp36-cp36m-manylinux1_i686.whl (665.3 kB view details)

Uploaded CPython 3.6m

threaded-0.9.1-cp35-none-win_amd64.whl (186.8 kB view details)

Uploaded CPython 3.5 Windows x86-64

threaded-0.9.1-cp35-none-win32.whl (160.2 kB view details)

Uploaded CPython 3.5 Windows x86

threaded-0.9.1-cp35-cp35m-manylinux1_x86_64.whl (689.3 kB view details)

Uploaded CPython 3.5m

threaded-0.9.1-cp35-cp35m-manylinux1_i686.whl (648.8 kB view details)

Uploaded CPython 3.5m

threaded-0.9.1-cp34-none-win_amd64.whl (173.4 kB view details)

Uploaded CPython 3.4 Windows x86-64

threaded-0.9.1-cp34-none-win32.whl (159.1 kB view details)

Uploaded CPython 3.4 Windows x86

threaded-0.9.1-cp34-cp34m-manylinux1_x86_64.whl (705.4 kB view details)

Uploaded CPython 3.4m

threaded-0.9.1-cp34-cp34m-manylinux1_i686.whl (661.7 kB view details)

Uploaded CPython 3.4m

File details

Details for the file threaded-0.9.1.zip.

File metadata

  • Download URL: threaded-0.9.1.zip
  • Upload date:
  • Size: 303.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for threaded-0.9.1.zip
Algorithm Hash digest
SHA256 b5bab8e9a584d586242f63987fc8ece039930a69ab9f0232c7ad23af726a1c96
MD5 d37b8fb6795da2554367d86f5cf77eff
BLAKE2b-256 1482a2f2ce39e87030e2f39299dbe8970bc87f2829a0b8420276acc2bfaff2c8

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-py2-none-any.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-py2-none-any.whl
Algorithm Hash digest
SHA256 909b6dd935a1d6a7f1eb45f7fe30db52b357224e7c0b357d3c97ed334fca2534
MD5 725da54b6a4aefa47f1f346537352628
BLAKE2b-256 dfe8b9689926bb76221c39e585e6eacc31c01dc27ddd45d69df2dab368aa9fc7

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-cp36-none-win_amd64.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 621499cb0e25bc78ac597e737253f94325addda859e69175fe86aebb3a42f49f
MD5 1224fe689429b7a75694ddc64da5d2be
BLAKE2b-256 424057e042d8359849c2da8c8f5976d73a8366764be4c6c100742475ee6c890c

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-cp36-none-win32.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-cp36-none-win32.whl
Algorithm Hash digest
SHA256 1b037974d00322c8cf3a2f62ea8af826c4e2673462ffe95f77b2e57376d5780f
MD5 b4621b5e9e276964fd7027be2fd27176
BLAKE2b-256 938ebbbc84456e9b845c5c9d63d5bacd01272d817f0bae485c8b1cd3fc7bf37b

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0cb684cc31ecf3d95c7cee3bfa00b4e8b9b29f2324efc4cc516eb58548ccd037
MD5 ef4bb83962320ff742ad60c94179d681
BLAKE2b-256 bd75c6fd0f183d5ceada96c7e02fe9af45be1b093e1a5ecaf36fadfe365b3d1c

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 a2cb47376a19fdaf4ab565567fbf144e173e35bbd85ac064875f37a39a343658
MD5 3c4087b04ba0cf5bbc769e682c25ca86
BLAKE2b-256 9f85be339c722a7fe8475c95c4f4137ea50415adbd13be52448a544eed4ef98d

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-cp35-none-win_amd64.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-cp35-none-win_amd64.whl
Algorithm Hash digest
SHA256 1f88be6757ec33f74a69246d2f6ef247fef4479fdaa33c68d25adc12e9509f53
MD5 f926bbed4901771b0abfbcb55b46fd3c
BLAKE2b-256 b87482752f107ca69400466d24f26e297b3e9efbf35da4b815302b79b864fee0

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-cp35-none-win32.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-cp35-none-win32.whl
Algorithm Hash digest
SHA256 b02dec630bdc29164e78e7e8ac91c801f373e83e88c3f9bbadc5540ba5ed3d14
MD5 7b1c6a93570715b0da25e579054d1f4b
BLAKE2b-256 b27cfcb4f7379f373f82522102bfd0f571ebad42b60ff9dd16db9fcfaa795f57

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 afa629c511d6dbfcee12da708f176ddf8156edaffe53230165dbed629da263b2
MD5 3495608842514c0c22a7e88d8e290f9e
BLAKE2b-256 6c06bb331fabdbc20ed04eb168d2c16c05631d53c93165624df697f73f0b2d02

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 1a20454a66fbfdfdc1461f2f8b3ed0cd814dad3531619021ceb57a95f5dc19cf
MD5 f22500fd3028177e4590fb5b0d3c34fb
BLAKE2b-256 99e3689e349f6fb937c3eb69aec11028d542a3a0ef19486da031ee93d1021d38

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-cp34-none-win_amd64.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-cp34-none-win_amd64.whl
Algorithm Hash digest
SHA256 a16f531cf018d76b956b0709779304af104a4c775154a4e7bc30b636d6be42b4
MD5 5e769421987493684b5c90f56aec8245
BLAKE2b-256 76b39e413db6fb71267c8f165d05a63cb5b30a382ce9fd83060fdb10aef63ccf

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-cp34-none-win32.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-cp34-none-win32.whl
Algorithm Hash digest
SHA256 f38994acc5c7879cd9c11f2595bbdb3c91504a163024d110ed1e4a50c88c327b
MD5 58c26a3071262bb13c71c517119da695
BLAKE2b-256 333692849d346ec257d7d89557cf37c2564f523705edae1ec685969cf028971a

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 db86e22a152adb9377ca46a74b2354630a9361b5f80a51718542139d619a1af1
MD5 eba1adc5fe4b8f6f2a2cfa0b3f9f786a
BLAKE2b-256 a8f28f1635548ce14fc9855c0876025ba6a7ae3b3816c4e0005269eaf141aabd

See more details on using hashes here.

File details

Details for the file threaded-0.9.1-cp34-cp34m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for threaded-0.9.1-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 e32c673462c140c8e3ecc4b5ee4db6ba700b340c1ce2b6a10982968dab6255a1
MD5 9d9a0f62eab8afab9ec26c48f9e204c0
BLAKE2b-256 64e0fc957c9a45f0299dda3f6a8f2169183f87db9d2b85ab7bb649e4068e4edd

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