Skip to main content

Quick and simple multithreading

Project description

Ready Thready Go

Ready Thready Go (rtgo) is a Python module for easily running Python functions utilising multi-threading.

Installation

Ready Thready Go is available through PyPI as rtgo. It can be installed via pip:

pip install rtgo

Requirements

No additional packages are required to run Ready Thready Go.

Python 3.6 or higher is recommended.

Usage

To use Ready Thready Go, the module ReadyThready should first be imported from the rtgo package.

The ReadyThready function 'go' is used to run a function with multiple different input arguments over multiple threads.

ReadyThready.go takes the arguments:

  • func, the name of the function to be run
  • args, a list of arguments to be passed to the function, including a list of multiple different arguments
  • arg_data_index, the position of the list of multiple arguments within the args list
  • n_threads, the number of threads to be used

It may be helpful to think of ReadyThready.go as an alternative to using a for loop to pass arguments from a list to a function: instead of

def my_function(number):
    new_number = (number * 3) - 1 
    return new_number

my_list = [1,2,3,4,5]

for x in my_list:
    my_function(x)

It is possible to supply the list and the function to ReadyThready.go and have it run in parallel over a specified number of threads:

from rtgo import ReadyThready
def my_function(number):
    new_number = (number * 3) - 1 
    return new_number

my_list = [1,2,3,4,5]

ReadyThready.go(func=my_function, args=[my_list], arg_data_index = 0, n_threads = 4)

Examples

Divide data and process a function on multiple threads

from rtgo import ReadyThready

# initialise variables
my_input_list = [1,2,3,4,5,6,7,8]
example_string = "example:"

# define a function to be run over multiple threads
def my_example_func(number, phrase):
    if number < 5:
        print(f"{phrase} {number} is less than 5")

# run the function over 4 threads
ReadyThready.go(func=my_example_func, args=[my_input_list, example_string], arg_data_index=0, n_threads=4)

Execute multiple functions concurrently

from rtgo import ReadyThready
import requests

# Retrieve Human Phenotype Ontology data
def get_hpo_data():
    my_hpo_data = requests.get("http://hpo.jax.org/api/hpo/term/HP%3A0001166/genes")
    my_hpo_data = my_lengthy_processing(my_hpo_data)
    return my_hpo_data

# Retrieve Medical Subject Headings data
def get_mesh_data():
    my_mesh_data = requests.get("https://id.nlm.nih.gov/mesh/lookup/term?label=obesity&match=contains&limit=10")
    my_mesh_data = my_lenghty_processing(my_mesh_data)
    return my_mesh_data

# run each function on a separate thread
ontology_data = ReadyThready.go_cluster([get_hpo_data, get_mesh_Data])

Using your project's logger

from rtgo import ReadyThready
import logging 

# create a logger for your project 
my_project_logger = logging.getLogger('example')
my_project_logger.setLevel(logging.INFO)
my_project_logger.addHandler(logging.StreamHandler())

# initialise variables
my_input_list = [1,2,3,4,5,6,7,8]
example_string = "example:"

# a simple function
def my_example_func(number, phrase):
    if number < 5:
        print(f"{phrase} {number} is less than 5")

# set ReadyThready to use your project logger
ReadyThready.set_logger(my_project_logger)

# run the function over 8 threads
ReadyThready.go(func=my_example_func, args=[my_input_list, example_string], arg_data_index=0, n_threads=8)

Authors

Thomas Rowlands, Emma Croot

Licence

GNU (see LICENCE.txt)

Change log

###version 0.0.5:

First stable version

###version 0.0.6: Improved error handling and logging

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

rtgo-0.0.7.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

rtgo-0.0.7-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file rtgo-0.0.7.tar.gz.

File metadata

  • Download URL: rtgo-0.0.7.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

File hashes

Hashes for rtgo-0.0.7.tar.gz
Algorithm Hash digest
SHA256 1a668c966d7b0033188ff99b621da2e15494fe16b29f529a286c7a53f92ceb5a
MD5 34f3bf972dba44eef1117a1bf0efd2b2
BLAKE2b-256 74750500ae95ad260ffc6ab0dd971403d80ef01674485b773998f9f99d5ecc7e

See more details on using hashes here.

File details

Details for the file rtgo-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: rtgo-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 18.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

File hashes

Hashes for rtgo-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 24c7c3c67ce8450147586b74cf4c631515ecf4b51f4c50d365d30b77e04b93f4
MD5 0d53d091314cc5feb3ab7c73d8cead5e
BLAKE2b-256 757043092867e56cc55bd866fd7c37c2a03ec1e7860186769164f89df3d04f93

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