aiopyramid icon indicating copy to clipboard operation
aiopyramid copied to clipboard

Consider adding blocking helper functions

Open dfee opened this issue 8 years ago • 1 comments

I've done some work to ensure blocking work won't happen in the main thread. You might consider adding this (or something similar) to aiopyramid or the docs:

import os
import sys
import functools
import threading

from pyramid.settings import aslist


class BlockingException(Exception):
    pass


def ensure_executor(f):
    '''Ensures that calls to the wrapped function won't be executed unless
    in an executor'''

    mt_ident = threading.get_ident()
    script_name = os.path.basename(sys.argv[0])

    @functools.wraps(f)
    def with_executor(request, *args, **kwargs):
        ct_ident = threading.get_ident()
        excluded_scripts = aslist(request.registry.settings.get(
            'aiopyramid.scripts_excluding_ensure_executor', ''
        ))
        if ct_ident != mt_ident or script_name in excluded_scripts:
            return f(request, *args, **kwargs)
        raise BlockingException(
            "You tried to call a blocking function from within the "
            "main thread. Run this function in an executor instead."
        )
    return with_executor

This allows for an additional setting in config.ini that looks like:

aiopyramid.scripts_excluding_ensure_executor =
    pshell
    proutes

And example usage looks like:

@ensure_executor
def get_session(request):
    session = maker()
    register(session, transaction_manager=request.tm)
    return session

dfee avatar Oct 19 '16 20:10 dfee

This looks great. Will you submit a PR to put this in helpers? Also, if you can write a test for this, that would be awesome. Then I can merge the PR and run the test and update the library.

housleyjk avatar Oct 24 '16 16:10 housleyjk