2016-09-11 5 views
0

Kann ein Modul in einer Sellerie-Task dynamisch importiert werden?Dynamisches Importieren eines Moduls in eine Sellerie-Task

Zum Beispiel habe ich ein Modul hello.py in meinem Arbeitsverzeichnis genannt:

$ cat hello.py 
def run(): 
    print('hello world') 

ich es dynamisch mit importlib importieren:

$ python3 
Python 3.5.2 (default, Jul 5 2016, 12:43:10) 
[GCC 5.4.0 20160609] on linux 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import importlib 
>>> p = importlib.import_module('hello') 
>>> p.run() 
hello world 

ich die gleichen wie oben von a tun will Sellerie Aufgabe:

@celery.task() 
def exec_module(module, args={}): 
    print('celery task running: %s' % (module)) 
    print(args) 
    print(os.getcwd()) 
    print(os.listdir()) 

    module = importlib.import_module('hello') 
    module.run() 

Ich bekomme die folgenden Fehler oder wenn die Aufgabe ausgeführt wird:

[2016-09-11 17:51:48,132: WARNING/MainProcess] [email protected] ready. 
[2016-09-11 17:52:05,516: INFO/MainProcess] Received task: web.tasks.exec_module[f24e3d03-5a17-41dc-afa9-541c99b60a35] 
[2016-09-11 17:52:05,518: WARNING/Worker-1] celery task running: example_module 
[2016-09-11 17:52:05,518: WARNING/Worker-1] {'example_file': '/tmp/tmpo4ks9ql0.xml', 'example_string': 'asd'} 
[2016-09-11 17:52:05,518: WARNING/Worker-1] /home/user/Learning/vision-boilerplate 
[2016-09-11 17:52:05,518: WARNING/Worker-1] ['run.py', 'requirements.txt', 'gulpfile.js', 'tsconfig.json', 'typings.json', 'package.json', 'vision_modules', 'typings', 'web', 'config', 'hello.py', 'docker-compose.yml', 'ng', 'node_modules', 'Dockerfile'] 
[2016-09-11 17:52:05,523: ERROR/MainProcess] Task web.tasks.exec_module[f24e3d03-5a17-41dc-afa9-541c99b60a35] raised unexpected: ImportError("No module named 'hello'",) 
Traceback (most recent call last): 
    File "/usr/local/lib/python3.5/dist-packages/celery/app/trace.py", line 240, in trace_task 
    R = retval = fun(*args, **kwargs) 
    File "/home/user/Learning/vision-boilerplate/web/__init__.py", line 21, in __call__ 
    return TaskBase.__call__(self, *args, **kwargs) 
    File "/usr/local/lib/python3.5/dist-packages/celery/app/trace.py", line 438, in __protected_call__ 
    return self.run(*args, **kwargs) 
    File "/home/user/Learning/vision-boilerplate/web/tasks.py", line 19, in exec_module 
    module = importlib.import_module('hello') 
    File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 986, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 969, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked 
ImportError: No module named 'hello' 

Hinweis vom Ausgang der print(os.listdir()) (Linie 6 in der Debug-Ausgabe), dass wir auf jeden Fall im richtigen Verzeichnis sind und hello.py rechts ist.

Ist es möglich, ein Modul dynamisch aus einer Sellerie-Task zu importieren?

Antwort

1

Ist es möglich, ein Modul in einer Sellerie-Aufgabe dynamisch zu importieren?

Ja. Weil ich es getan habe. Wenn das Modul einzeln ist und keinem Paket angehört, sollten Sie das Verzeichnis des Moduls dem sys-Pfad hinzufügen.

sys.path.append ('Pfad des Moduls')

Zum Beispiel:

import importlib 
import os 
import sys 
import celery 
from demo import celeryconfig 

# add your path to the sys path 
sys.path.append(os.getcwd()) 

app = celery.Celery('tasks',) 
app.config_from_object(celeryconfig) 


@celery.task() 
def exec_module(module, args={}): 
    print('celery task running: %s' % (module)) 
    print(args) 
    print(os.getcwd()) 
    print(os.listdir()) 

    module = importlib.import_module('hello') 
    module.run() 

Ich hoffe es für Sie hilfreich sein. Danke

Verwandte Themen