I have a running celery server using redis as the borker and result storage (python3). I would like to have an arbitrary function, which has not been registered to the server, being executed by a celery worker. I tried to serialize this function using the package marshal (following Is there an easy way to pickle a python function (or otherwise serialize its code)?) and transfered the bytecode to a worker:
celery_server.py:
from celery import Celery
import types
import marshal
app = Celery('tasks', broker='redis://guest@localhost//', backend='redis://localhost')
@app.task
def run_fct( fct_code, args, kwargs ):
code = marshal.loads( fct.__code__ )
func = types.FunctionType(code, globals(), "some_func_name" )
return fct( *args, **kwargs )
client.py
from celery_server import run_fct
import marshal
def calc( x, y ):
return x*y
fct_code = marshal.dumps( calc.func_code )
run_fct.apply_async( (fct_code, 10, 2 ) )
I get the following error on the client side:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 0: invalid continuation byte
in the function bytes_to_str
in kombu.utils.encoding.py
.
Is there another or better way to have my function being executed?
Thank you for any help.