Skip to main content

Cloud Functions Dispatch

Cloud Functions Dispatch gives you the ability to dispatch in-process no-response function calls to a remote cloud function by applying a decorator to your function.

This is useful for tasks such as the following:

  • update a database record separately from this process
  • run secondary processes as side-effects of receiving a request
  • fan-out of a large job into chunks that are completed independently

Basically any task that accepts some set of serializable parameters but not any in-process state and does not need to return a value is a great candidate for dispatching.

Functions are asynchronously executed with no mechanism for returning a value to the caller. From the caller's perspective, these functions are "fire and forget". Support for functions that return values may be implemented in a future version.

Example

Imagine you have a function that you call every time a row in a certain table :

def save(users, context):
    for user in users:
        user.save()
        update_related(user, context)

def update_related(user, context):
    entity = db.retrieve(user.id)
    for item in entity.get_related():
        item.last_updated = context.update_time
        item.save()

Note that this code is intentionally inefficient just to demonstrate the point; imagine update_related takes 10 seconds per user and you might be doing this for thousands of users at a time. Your process will struggle for resources: time, memory, or threads will be consumed performing the work. Now imagine you are running a job like this every few seconds. Your code will choke.

One approach to handle this case is to send update_related to a separate cloud function for execution. But then you have to define the function, marshall data somehow, and then invoke the function. While these tasks are simple in theory, there are several complexities that make them challenging in practice.

Using cloud_functions_dispatch, the only change necessary to the code above is decorating update_related:

from cloud_functions_dispatch import dispatch

...

@dispatch  # <- this is the only change necessary to make this a remote call
def update_related(user, context):
    entity = db.retrieve(user.id)
    for item in entity.get_related():
        item.last_updated = context.update_time
        item.save()

Now every time you call update_related, the full parameter list is serialized, compressed, and pushed to PubSub. A cloud function also containing the same function will receive the message and execute the code.

Refer to the Project Documentation for installation and usage.

Metadata

Release files for cloud-functions-dispatch 0.9.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cloud-functions-dispatch 0.9.1
File Size Uploaded Provenance
cloud-functions-dispatch-0.9.1.tar.gz 183.3 kB Without provenance Details

Release files / cloud-functions-dispatch-0.9.1.tar.gz

Download URL cloud-functions-dispatch-0.9.1.tar.gz
Size 183.3 kB
Tags Source
SHA-256 checksum
How to use checksums
44c0cc0d2f6844d07b8aa6d8d245091009906df844821a602be5888e3495c703
BLAKE2b-256 checksum
How to use checksums
ea38b9a6000bcaa374a1dc00f34c5fd848455bab192ae264b4c31f485eb4694f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

Security for cloud-functions-dispatch 0.9.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page