Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
This library allows you to do your tasks in multiple threads easily.
This is helpful when you have a lot of data to process.
Assume that you have a large list of items to process. You need to write a producer to put items in the queue one by one.
Workers will get data from the queue and then process it. Putting data into a queue should be quicker than processing it (worker).
pip install threadingq
from threadingq import ThreadingQueue
def worker(data):
pass
async def worker2(data):
pass
a. Set the number of threads and the worker
b. Put data into the queue
You can also use ThreadingQueue as a context manager
def producer():
# Start the queue
with ThreadingQueue(40, worker) as tq:
...
tq.put(data)
async def producer():
# Start the queue
async with ThreadingQueue(40, worker) as tq:
...
await tq.put(data)
await producer()
or
asyncio.run(producer())
worker_params
log_dir
to store logs to fileworker_params_builder
to generate parameters for each worker.on_thread_close
is an optional param as a function that is helpful when you need to close the database connection when a thread done# 0.0.14
with ThreadingQueue(num_of_threads, worker) as tq:
...
await tq.put(data)
# From 0.0.15
# Sync
with ThreadingQueue(num_of_threads, worker) as tq:
...
tq.put(data)
# Async
async with ThreadingQueue(num_of_threads, worker) as tq:
...
await tq.put(data)
asyncio.sleep
to wait when the queue is full compared to time.sleep
in the sync version. In most cases, the difference in performance is not much.import json
import pymysql
import asyncio
from threadingq import ThreadingQueue
NUM_OF_THREADS = 40
def get_db_connection():
return pymysql.connect(host='localhost',
user='root',
password='123456',
database='example',
cursorclass=pymysql.cursors.DictCursor)
# Build params for the worker, the params will be persistent with the thread
# This function is called when init a new thread or retry
def worker_params_builder():
# Threads use db connection separately
conn = get_db_connection()
conn.autocommit(1)
cursor = conn.cursor()
return {"cursor": cursor, "connection": conn}
# To clear resources: close database connection, ...
# This function is called when the thread ends
def on_close_thread(cursor, connection):
cursor.close()
connection.close()
def worker(image_info, cursor, uid: int, **kwargs):
# Update image info into database
sql = "UPDATE images SET width = %s, height = %s, uid = %s WHERE id = %s"
cursor.execute(sql, (image_info["width"], image_info["height"], uid, image_info["id"]))
def producer(source_file: str):
with ThreadingQueue(
NUM_OF_THREADS, worker,
log_dir=f"logs/update-images",
worker_params_builder=worker_params_builder,
on_close_thread=on_close_thread,
params={"uid": 123},
retry_count=1
) as tq:
with open(source_file, 'r') as f:
for line in f:
if not line:
continue
data = json.loads(line)
tq.put(data)
if __name__ == "__main__":
producer("images.jsonl")
src/threadingq/__version__.py
python3 -m build
python3 -m twine upload dist/*
FAQs
Threading Queue
We found that threadingq demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.