Skip to main content

快速简单的多线程

项目描述

准备好 Thready Go

Ready Thready Go (rtgo) 是一个 Python 模块,用于利用多线程轻松运行 Python 函数。

安装

Ready Thready Go 可通过 PyPI 作为 rtgo 获得。它可以通过 pip 安装:

pip install rtgo

要求

运行 Ready Thready Go 不需要额外的软件包。

建议使用 Python 3.6 或更高版本。

用法

要使用 Ready Thready Go,首先应从 rtgo 包中导入模块 ReadyThready。

ReadyThready 函数“go”用于在多个线程上运行具有多个不同输入参数的函数。

ReadyThready.go 接受参数:

  • func,要运行的函数的名称
  • args,要传递给函数的参数列表,包括多个不同参数的列表
  • arg_data_index, 多个参数列表在 args 列表中的位置
  • n_threads,要使用的线程数

将 ReadyThready.go 视为使用 for 循环将参数从列表传递给函数的替代方法可能会有所帮助:而不是

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)

可以将列表和函数提供给 ReadyThready.go 并让它在指定数量的线程上并行运行:

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)

例子

在多个线程上划分数据并处理一个函数

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)

同时执行多个功能

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])

使用项目的记录器

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)

作者

托马斯·罗兰兹,艾玛·克鲁特

执照

GNU(见 LICENCE.txt)

更改日志

###版本 0.0.5:

第一个稳定版本

###version 0.0.6:改进的错误处理和日志记录

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

rtgo-0.0.7.ta​​r.gz (5.3 kB 查看哈希)

已上传 source

内置分布

rtgo-0.0.7-py3-none-any.whl (18.0 kB 查看哈希

已上传 py3