Skip to main content

Socket.IO server to schedule Celery tasks from clients in real-time.

Project description

PyPI version Docker Image Version (latest semver)

Stirfried 🥡

Socket.IO server to schedule Celery tasks from clients in real-time.

Getting started

Stirfried has a three layered architecture:

  1. Celery workers
  2. Socket.IO server
  3. Socket.IO clients

Installation

You really only need to install Stirfried in your Celery workers via pip:

pip install stirfried

For the Socket.IO server component you can use the prebuilt docker image korijn/stirfried, or you can copy the project and customize it to your liking (there's only about 40 lines of code in the server).

Clients can connect using standard Socket.IO libraries.

Celery workers

In your Celery workers, import the StirfriedTask:

from stirfried.celery import StirfriedTask

Configure StirfriedTask as the base class globally:

app = Celery(..., task_cls=StirfriedTask)

...or per-task:

@app.task(base=StirfriedTask)
def add(x, y, room=None):
    return x + y

Rooms

The client passes the room to emit to via the keyword argument room.

The StirfriedTask base class depends on the presence of this keyword argument.

This means you are required to add the keyword argument room=None to your task definitions in order to receive it.

It also gives the client control over whether the task results and progress updates should be emitted to them or a certain room.

Progress

You can emit progress from tasks by calling self.emit_progress(current, total).

Note that you are required to pass bind=True to the celery.task decorator in order to get access to the self instance variable.

@celery.task(bind=True)
def add(self, x, y, room=None):
    s = x
    self.emit_progress(50, 100)  # 50%
    s += y
    self.emit_progress(100, 100)  # 100%
    return s

Socket.IO server

You are required to provide a settings.py file with the configuration for the server.

It requires at a minimum:

  • socketio_redis - Redis connection string for the Socket.IO server.
  • broker_url - Connection string for the Celery broker.

The server sends tasks to the Celery broker by name, so it can act as a gateway to many different Celery workers with different tasks. You can leverage Celery's task routing configuration for this purpose.

Example

Let's say you have two workers, one listening on the feeds queue and another on the web queue. This is how you would configure the server accordingly:

socketio_redis = "redis://localhost:6379/0"
broker_url = "redis://localhost:6379/1"
task_routes = {
    "feed.tasks.*": {"queue": "feeds"},
    "web.tasks.*": {"queue": "web"},
}

Docker image

You can build the docker image and run it locally as follows (note that you need to create a settings.py file):

docker run --rm -ti -v `pwd`/settings.py:/app/settings.py:ro -p 8000:8000 stirfried

Socket.IO clients

TODO

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

stirfried-0.2.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

stirfried-0.2.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file stirfried-0.2.0.tar.gz.

File metadata

  • Download URL: stirfried-0.2.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.8 Windows/10

File hashes

Hashes for stirfried-0.2.0.tar.gz
Algorithm Hash digest
SHA256 22cdbbc7a30252e2518707cc8d435522fd769b4eb8f5cd256e60a62a79302475
MD5 f0fd0fdee8e91d9ddfa33cca82164b6d
BLAKE2b-256 a7a27f949de43a6332a715739dd8710b0049a3975d6ae044a4471391715e19da

See more details on using hashes here.

File details

Details for the file stirfried-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: stirfried-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.8 Windows/10

File hashes

Hashes for stirfried-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a76b8e6d76a08e23d506e946fa349bcb8b63cf15ff4ed961a6f93c7176394da4
MD5 fee18450c464929d062124d6fd51d596
BLAKE2b-256 0a288372b137233020a521e2068a15d799e7d09df7b905020b22567e43602b96

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