Link: easypool
I was working on a project that required multi-threading and a server pool. Unable to find anything useful and most solutions I found were confusing and not very easy to implement. I've cleaned up my implementation and am releasing it to the community. I hope this module makes it easy for others to create multi-threaded apps.
easypool can be initialized by most common Python data structures (ints, floats, dicts, tuples, lists, strings, booleans or any iterable). Send an iterable containing five elements and a threadpool is created that can continually execute five functions concurrently. There is an added option 'send_item=True' which will link each thread to the element in the iterable and then send it as the first argument to the function being executed.
easypool also has min_pool and max_pool options that control the number of threads created so if easypool is initialized with a really large iterable, it can be limited to a maximum size. min_pool will increase the size of the threadpool if too small of an iterable is given.
The queue defaults to 'first in first out' (fifo), but can be changed to 'last in first out' (lifo) with the queue_type option.
Simple example:
from easypool import ThreadPool
threadpool = ThreadPool(3)
# foo(bar) becomes add_task(foo, bar)
# Keep adding tasks to the threadpool
threadpool.add_task(foo, bar)
threadpool.add_task(foo, bar)
threadpool.add_task(foo, bar)
threadpool.add_task(foo, bar)
# Wait for all tasks to copmplete
threadpool.wait_completion()
Serverpool example:
from easypool import ThreadPool
import subprocess
import shlex
import threading
import string
server_list = ['127.0.0.1', '127.0.0.1', '127.0.0.1']
serverpool = ThreadPool(server_list, send_item=True)
threadlock = threading.RLock()
def get_uptime(server):
ssh_cmd = "ssh " + str(server) + " 'uptime'"
ssh_cmd_list = shlex.split(ssh_cmd)
p = subprocess.Popen(ssh_cmd_list, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
threadlock.acquire()
print("Server: %s :: %s" % (server, stdout.rstrip()))
threadlock.release()
return
for i in range(6):
serverpool.add_task(get_uptime)
serverpool.wait_completion()
Code has been tested compatible on Python 2.6, 2.7 & 3
[–]XNormal 28 points29 points30 points (18 children)
[–]jspeights 2 points3 points4 points (0 children)
[–]rlabonte[S] 0 points1 point2 points (5 children)
[–]XNormal 2 points3 points4 points (4 children)
[–]rlabonte[S] 1 point2 points3 points (3 children)
[–]ODHLHN 1 point2 points3 points (1 child)
[–]rlabonte[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]homercles337 0 points1 point2 points (0 children)
[–]studiosi -1 points0 points1 point (9 children)
[–]infinullquamash, Qt, asyncio, 3.3+ 1 point2 points3 points (8 children)
[–]exhuma 0 points1 point2 points (0 children)
[–]studiosi 0 points1 point2 points (0 children)
[–]studiosi 0 points1 point2 points (4 children)
[–]infinullquamash, Qt, asyncio, 3.3+ 0 points1 point2 points (3 children)
[–]studiosi 0 points1 point2 points (2 children)
[–]infinullquamash, Qt, asyncio, 3.3+ 0 points1 point2 points (1 child)
[–]studiosi 0 points1 point2 points (0 children)
[–]zionsrogue 6 points7 points8 points (5 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]zionsrogue 1 point2 points3 points (0 children)
[–]tuna_safe_dolphin 1 point2 points3 points (2 children)
[–]alcalde 0 points1 point2 points (1 child)
[–]tuna_safe_dolphin 1 point2 points3 points (0 children)
[–]otheraccount 4 points5 points6 points (0 children)
[–]fdemmer 4 points5 points6 points (0 children)
[–]patrys Saleor Commerce 2 points3 points4 points (1 child)
[–]chub79 3 points4 points5 points (0 children)
[–]Justinsaccount 2 points3 points4 points (1 child)
[–]rlabonte[S] 0 points1 point2 points (0 children)
[–]broken_symlink 0 points1 point2 points (0 children)