Skip to main content

A Simple High Performance Multiprocess Queue

Project description

QuasiQueue

QuasiQueue is a MultiProcessing library for Python that makes it super easy to have long running MultiProcess jobs. QuasiQueue handles process creation and cleanup, signal management, cross process communication, and all the other garbage that makes people hate dealing with multiprocessing.

QuasiQueue works by splitting the work into two components- the main process whose job it is to feed a Queue with work, and then read processes that take work off of the Queue to run. All the developers have to do is create two functions-

  • writer is called when the queue gets low. It should return an iterable (list, generator) that QuasiQueue uses to grow the multiprocess Queue.
  • reader is called once for each item in the Queue. It runs in completely different processes from the writer.
flowchart LR
  writer(writer)-->queue((queue))
  queue-->reader1(reader)
  queue-->reader2(reader)
  queue-->reader3(reader)
  queue-->reader4(reader)

These functions can be as simple or complex as you need.

import asyncio

from quasiqueue import QuasiQueue


async def writer(desired: int):
  """Feeds data to the Queue when it is low.
  """
  for x in range(0, desired):
    yield x



async def reader(identifier: int|str):
  """Receives individual items from the queue.

  Args:
      identifier (int | str): Comes from the output of the Writer function
  """
  print(f"{identifier}")


runner = QuasiQueue(
  "hello_world",
  reader=reader,
  writer=writer,
)

if __name__ == '__main__':
  asyncio.run(runner.main())

Use Cases

There are a ton of use cases for QuasiQueue.

WebServer

QuasiQueue could be the basis for a web server. The write function would need to feed sockets to the Queue, would would be picked up by the reader for handling.

flowchart LR

  subgraph Configuration
  http
  end

  subgraph Server
  http-->writer
  writer(writer)--socket-->queue((queue))
  queue--socket-->reader1(reader)
  queue--socket-->reader2(reader)
  queue--socket-->reader3(reader)
  queue--socket-->reader4(reader)
  end

Website Image Crawler

QuasiQueue could be used to crawl a website, or series of websites, to download data.

flowchart RL

  subgraph Crawler
  writer(writer)-->queue((queue))
  queue-->reader1(reader)
  end
  database(Links)--Stale or Unread Links-->writer
  reader1(reader)--Images-->FileSystem
  reader1(reader)--Found Links-->database

As new pages are found they get added to a database. The write pulls pages out of the database as the queue gets smaller, and the reader adds new pages that it finds to the database. The writer function can pull links that haven't been crawled at all, and once it runs out of those it can recrawl links based on their age.

Image Processor

QuasiQueue can be used to run large one off jobs as well, such as processing a list of images. If someone has several thousand images to process they can have the writer function feed the list into the Queue, and reader processes can take the files from the queue and run the processing needed.

flowchart LR

  subgraph Configuration
  filelist
  end

  subgraph ImageProcessor
  filelist-->writer
  writer(writer)-->queue((queue))
  queue-->reader1(reader)
  end
  reader1(reader)-->ProcessedFiles

Installation

pip install quasiqueue

Arguments

Name

The first argument when initializing QuasiQueue is the name of the queue. This is used when naming new processes (which makes logging and ps commands a lot more useful)

Reader

The reader function is called once per item in the queue.

async def reader(identifier: int|str):
  """Receives individual items from the queue.

  Args:
      identifier (int | str): Comes from the output of the Writer function
  """
  print(f"{identifier}")

The reader can be extremely simple, as this one liner shows, or it can be extremely complex.

The reader can be asynchronous or synchronous. When the reader is an async function the concurrent_tasks_per_process setting can be used to control how many reader tasks will run per process. For example, if you have four processes running and allow ten concurrent tasks then the reader function will have 40 instances running. This can be beneficial if your reader function requires a lot of independent IO (such as disk writes or HTTP lookups), but if your reader is primarily running calculations then it's less likely to benefit.

Writer

The write function is called whenever the Queue is low. It has to return an iterator of items that can be pickled (strings, integers, or sockets are common examples) that will be feed to the Reader. Generators are a great option to reduce memory usage, but even simple lists can be returned. The writer function has to be asynchronous.

The writer function only has one argument- the desired number of items that QuasiQueue would like to retrieve and add to the Queue. This number is meant to allow for optimization on behalf of the developers- it can be completely ignored, but QuasiQueue will run more efficiently if you keep it as close the desired as possible.

async def writer(desired: int):
  """Feeds data to the Queue when it is low.
  """
  return range(0, desired)

In the event that there are no items available to put in the Queue the write function should return None. This will signal to QuasiQueue that there is nothing for it, and it will add a slight (configurable) delay before attempting to retrieve more items.

QuasiQueue will prevent items that were recently placed in the Queue from being requeued within a configurable time frame. This is meant to make the write function more lenient- if it happens to return duplicates between calls QuasiQueue will just discard them.

Context

The context function is completely optional. It runs once, and only once, when a new reader process is launched. It is used to initialize resources such as database pools so they can be reused between reader calls.

If the function is provided it should return a dictionary. The reader function will need to have a context argument, which will be the results from the context function. The context function can be asynchronous or synchronous.

def context():
  ctx = {}
  ctx['http'] = get_http_connection_pool()
  ctx['dbengine'] = get_db_engine_pool()
  return ctx

def reader(identifier: int|str, ctx: Dict[str, Any]):
  """Receives individual items from the queue.

  Args:
      identifier (int | str): Comes from the output of the Writer function
      ctx (Dict[str, Any]): Comes from the output of the Context function
  """
  ctx['dbengine'].execute("get item")
  ctx['http'].get("url")
  print(f"{identifier}")


runner = QuasiQueue(
  "hello_world",
  reader=reader,
  writer=writer,
  context=context
)

Although this function is not required it can have amazing performance implications. Connection pooling of databases and websites can save a remarkable amount of resources on SSL handshakes alone.

Settings

QuasiQueue has a variety of optimization settings that can be tweaked depending on usage.

Name Type Description Default
empty_queue_sleep_time float The time in seconds that QuasiQueue will sleep the writer process when it returns no results. 1.0
full_queue_sleep_time float The time in seconds that QuasiQueue will sleep the writer process if the queue is completely full. 5.0
graceful_shutdown_timeout integer The time in seconds that QuasiQueue will wait for readers to finish when it is asked to gracefully shutdown. 30
lookup_block_size integer The default desired passed to the writer function. This will be adjusted lower depending on queue dynamics. 10
max_jobs_per_process integer The number of jobs a reader process will run before it is replaced by a new process. 200
concurrent_tasks_per_process integer How many async tasks can run at once inside a single process. 2
max_queue_size integer The max allowed size of the queue. 300
num_processes integer The number of reader processes to run. 2
prevent_requeuing_time integer The time in seconds that an item will be prevented from being readded to the queue. 300
queue_interaction_timeout float The time QuasiQueue will wait for the Queue to be unlocked before throwing an error. 0.01

Settings can be configured programmatically, via environment variables, or both.

Environment Variables

All Settings can be configured via environment variables. The variables should start with the QuasiQueue name and an underscore. For example, if you named your QuasiQueue Acme then ACME_NUM_PROCESS would be used to set the number of processes.

Programmatic

There are two methods to programmatically define the settings.

The first one is to initialize the settings and override the specific ones.

from quasiqueue import Settings, QuasiQueue

QuasiQueue(
  "MyQueue",
  reader=reader,
  writer=writer,
  settings=Settings(lookup_block_size=50)
)

This method is simple, but the downside is that you lose the environment variable prefixes. So when using this method you have to set NUM_PROCESSES rather than MYQUEUE_NUM_PROCESSES. The work around is to extend the Settings object to give it your desired prefix.

from quasiqueue import Settings, QuasiQueue
from pydantic_settings import SettingsConfigDict

class MySettings(Settings)
  model_config = SettingsConfigDict(env_prefix="MY_QUEUE_")
  lookup_block_size: int = 50

QuasiQueue(
  "MyQueue",
  reader=reader,
  writer=writer,
  settings=MySettings()
)

Accessing Settings

The reader, writer, and context functions can optionally retrieve a copy of the QuasiQueue settings in use by defining a settings argument in their function.

async def reader(item: Any, settings: Dict[str, Any])
  print(settings['project_name'])

If you create a custom Settings class, as in the programmatic example, you can add your own fields that will be passed to your QuasiQueue functions.

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

quasiqueue-0.5.0.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quasiqueue-0.5.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file quasiqueue-0.5.0.tar.gz.

File metadata

  • Download URL: quasiqueue-0.5.0.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for quasiqueue-0.5.0.tar.gz
Algorithm Hash digest
SHA256 c8a97c30e2c975383101b887e54f829d64e26a45c233d8d76fb6a6c5024c5815
MD5 b98f03c170b4918e3906551eb2e922ea
BLAKE2b-256 88f925678152f1765f0c285e4e387dbf2ebf390a7a064d37fee028914bf2c23f

See more details on using hashes here.

Provenance

The following attestation bundles were made for quasiqueue-0.5.0.tar.gz:

Publisher: pypi.yaml on tedivm/quasiqueue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quasiqueue-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: quasiqueue-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for quasiqueue-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a77373b68e1ac42ec376b69292f5b1aa85bbd5fc8fea49d97e55cc0ee9af9875
MD5 65f8c7e639594ae8c88286319a2b8fbd
BLAKE2b-256 dc567143e984c49fc94104519399585490e907c30fb7b8a04f16cc9beada27bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for quasiqueue-0.5.0-py3-none-any.whl:

Publisher: pypi.yaml on tedivm/quasiqueue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page