Skip to main content

Flask extension that provides an easy-to-implement controller for multiprocessing tasking. It provides default functions such as task-queueing, health-check, status-check, manual-stop and process-safe logger.

Project description

Flask-Multiprocess-Controller is an extension for Flask that provides an easy-to-implement controller for multiprocessing tasking. It provides default functions such as task-queueing, health-check, status-check, manual-stop and process-safe logger.

A common usage scenario is one has some tasks which are computational expensive and require multiple instances running concurrently. This controller provides a separate layer of controller by creating controlling thread for each instance. This can greatly improve level-of-control for computational expensive tasks.

Features

Flask-Multiprocess-Controller provides the MetaMPController() class which provides default controlling method to control the execute() method in the linked, child of MetaMPTask() class. Default controlling methods are as follows: - GET: retrieve the current status of certain task in sub-process - POST: put an execution request to the queue waiting to be executed when there is a new slot for a new sub-process - DELETE: sending the stop signal to certain task in sub-process gently

All Flask supported HTTP methods are supported(with necessary overrides)

One can also use MetaMPResource() class to adopt Flask-RESTful style APIs.

Example: Build a minimal Controller-Task structure

In the following example, we will use the built-in TemplateMPController, and TemplateMPResource to build the structure of controller-task framework. One may need to override some methods in MetaMPController for more functionality.

import logging
from flask import Flask
from flask_restful import Api
from flask_multiprocess_controller import TemplateMPController, TemplateMPResource, MetaMPTask

class SampleTask(MetaMPTask):

    def execute(self) -> None:
        task_logger = logging.getLogger(str(os.getpid()))
        counter = 0
        while counter < 100:
            counter += 1
            task_logger.debug("doing some simple task, progress {}%.".format(counter))
            time.sleep(0.5)
            self.upload_status(counter)
            self.checkpoint()

sample_api = Api()
sample_controller = TemplateMPController(target_task=SampleTask, max_num_process=2)
sample_api.add_resource(TemplateMPResource, '/sample', resource_class_args=(sample_controller,))

app = Flask('Sample')
sample_api.init_app(app)
app.run()

Installation

The easiest way to install it is using pip from PyPI

pip install flask-multiprocess-controller

License

See the LICENSE file for license rights and limitations (BSD-3-Clause).

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

Flask-Multiprocess-Controller-0.1.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file Flask-Multiprocess-Controller-0.1.tar.gz.

File metadata

File hashes

Hashes for Flask-Multiprocess-Controller-0.1.tar.gz
Algorithm Hash digest
SHA256 8e21c9179b307f5f8a0c3e61c07b17011ca1691ae0d03f6bf9aede5362e1d0a0
MD5 cda4817f61c849abb71f9d11eb2427e1
BLAKE2b-256 318e1903625f0145c0f051d525047f41f966e8671d68236cb205f4335c421f83

See more details on using hashes here.

File details

Details for the file Flask_Multiprocess_Controller-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for Flask_Multiprocess_Controller-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 02617aa29b38ac9d37a1f33fbe1e7cf5df2fb002a576375b45fb188c4fbcdb81
MD5 0f15d4330a16e2d350d8afaac2eafa9a
BLAKE2b-256 2db07ab35d0bfe5efc8a59b13b82f566a148c2d49cbe1fa91458671f59840490

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