Marco De Paoli | 10 May 2012 14:33
Picon
Gravatar

a little trick for uwsgidecorators

When you execute your python files outside uwsgi context (ex. for test purposes) you are not able to import uwsgidecorators module
Because you get an ImportError: No module named uwsgi
This could be annoying.
I use a little trick to get out of this.
(The code refers the spool decorator but you can get the idea and apply it to the others)

try:
    from uwsgidecorators import spool
except ImportError:
    def spool(f):
        f.spool = f
        return f

This way, when you test, you can make the same call but obtaining a synchronous behaviour in place of a deferred one.
Obviously, in production (when you are running in uwsgi "shell") you obtain the uwsgi standard deferred behaviour

Hope it can be useful for someone,
Marco

_______________________________________________
uWSGI mailing list
uWSGI@...
http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi
Riccardo Magliocchetti | 10 May 2012 14:39
Picon

Re: a little trick for uwsgidecorators

Hello,

Il 10/05/2012 14:33, Marco De Paoli ha scritto:
> When you execute your python files outside uwsgi context (ex. for test
> purposes) you are not able to import uwsgidecorators module
> Because you get an ImportError: No module named uwsgi
> This could be annoying.
> I use a little trick to get out of this.
> (The code refers the spool decorator but you can get the idea and apply it
> to the others)
>
> try:
>      from uwsgidecorators import spool
> except ImportError:
>      def spool(f):
>          f.spool = f
>          return f
>
> This way, when you test, you can make the same call but obtaining a
> synchronous behaviour in place of a deferred one.
> Obviously, in production (when you are running in uwsgi "shell") you obtain
> the uwsgi standard deferred behaviour

I don't know how it would behave in your specific case but you can also 
take a look at contrib/upython, see:
http://www.mail-archive.com/uwsgi-FfzAktRlpg7/cILp9QSAqw <at> public.gmane.org/msg02994.html

cheers,
riccardo
Ryan Showalter | 11 May 2012 00:29
Picon
Gravatar

Re: a little trick for uwsgidecorators

Hey Guys,

The spooler can be sort of cumbersome to use sometimes due to it's
limitation of string-only arguments.  I've created the following
method that I use for development and production.  It allows me to run
any function in the spooler that's not a class method and pass any
arguments to it without worrying about picking beforehand.  It
executes synchronously on django's devserver and asynchronously when
the uwsgi module is available.

def make_async(func, *args, **kwargs):
    try:
        from custom.uwsgi_utils import _make_async
        body = kwargs.pop('body', None)
        _make_async.spool(
            func=pickle.dumps(func),
            args=pickle.dumps(args),
            kwargs=pickle.dumps(kwargs),
            body=pickle.dumps(body))
    except ImportError:
        func(*args, **kwargs)

and then _make_async is defined as:

 <at> spool
def _make_async(args):
    """This should never be called directly, look at custom.utils.make_async"""

    func = pickle.loads(args['func'])
    fargs = pickle.loads(args['args'])
    fkwargs = pickle.loads(args['kwargs'])
    fbody = pickle.loads(args['body'])

    if fbody is not None:
        fkwargs.update({'body': fbody})

    func(*fargs, **fkwargs)

Example usage:

def my_fun(foo, bar=None, body=None):
  print foo, bar

  print body

make_async(my_fun, "Hello World", bar=1000, body="This can be a really
huge object")

Ryan-

On Thu, May 10, 2012 at 5:39 AM, Riccardo Magliocchetti
<riccardo.magliocchetti <at> gmail.com> wrote:
> Hello,
>
> Il 10/05/2012 14:33, Marco De Paoli ha scritto:
>
>> When you execute your python files outside uwsgi context (ex. for test
>> purposes) you are not able to import uwsgidecorators module
>> Because you get an ImportError: No module named uwsgi
>> This could be annoying.
>> I use a little trick to get out of this.
>> (The code refers the spool decorator but you can get the idea and apply it
>> to the others)
>>
>> try:
>>     from uwsgidecorators import spool
>> except ImportError:
>>     def spool(f):
>>         f.spool = f
>>         return f
>>
>> This way, when you test, you can make the same call but obtaining a
>> synchronous behaviour in place of a deferred one.
>> Obviously, in production (when you are running in uwsgi "shell") you
>> obtain
>> the uwsgi standard deferred behaviour
>
>
> I don't know how it would behave in your specific case but you can also take
> a look at contrib/upython, see:
> http://www.mail-archive.com/uwsgi <at> lists.unbit.it/msg02994.html
>
> cheers,
> riccardo
> _______________________________________________
> uWSGI mailing list
> uWSGI <at> lists.unbit.it
> http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi
_______________________________________________
uWSGI mailing list
uWSGI <at> lists.unbit.it
http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi

Gmane