0

I'm trying to use Python-RQ with Zope by calling an external method (for background tasks) from ZMI after a certain operation. The file called by external method resides in Extensions. It initialises connection to Redis and imports a module that runs the background tasks. The question is where should this to be imported file be placed ? Python-RQ does not seem to recognise if I put it inside Products directory. It throws no module named Products.xyz. Below is the code snippet

from redis import Redis
from rq import Queue
from Products.def_update_company_status import ae_update_company_status

q = Queue(connection=Redis())

def rq_worker(context):
    q.enqueue(ae_update_company_status)
    return 'DONE'

The rq_worker function is invoked by the external method.

Below is the error

        18:12:40 default: Products.def_update_company_status.ae_update_company_status() (4b2b5c81-e329-4031-a3e7-b9b1bb198278)
    18:12:40 ImportError: No module named Products.def_update_company_status
    Traceback (most recent call last):
      File "/home/zope/ams/lib/python2.6/site-packages/rq-0.6.0-py2.6.egg/rq/worker.py", line 588, in perform_job
        rv = job.perform()
      File "/home/zope/ams/lib/python2.6/site-packages/rq-0.6.0-py2.6.egg/rq/job.py", line 498, in perform
        try:
      File "/home/zope/ams/lib/python2.6/site-packages/rq-0.6.0-py2.6.egg/rq/job.py", line 206, in func
      File "/home/zope/ams/lib/python2.6/site-packages/rq-0.6.0-py2.6.egg/rq/utils.py", line 150, in import_attribute
        module = importlib.import_module(module_name)
      File "build/bdist.linux-x86_64/egg/importlib/__init__.py", line 37, in import_module
        __import__(name)
    ImportError: No module named Products.def_update_company_status
    Traceback (most recent call last):
      File "/home/zope/ams/lib/python2.6/site-packages/rq-0.6.0-py2.6.egg/rq/worker.py", line 588, in perform_job
        rv = job.perform()
      File "/home/zope/ams/lib/python2.6/site-packages/rq-0.6.0-py2.6.egg/rq/job.py", line 498, in perform
        try:
      File "/home/zope/ams/lib/python2.6/site-packages/rq-0.6.0-py2.6.egg/rq/job.py", line 206, in func

      File "/home/zope/ams/lib/python2.6/site-packages/rq-0.6.0-py2.6.egg/rq/utils.py", line 150, in import_attribute
        module = importlib.import_module(module_name)
      File "build/bdist.linux-x86_64/egg/importlib/__init__.py", line 37, in import_module
        __import__(name)
    ImportError: No module named Products.def_update_company_status
    18:12:40 Moving job to u'failed' queue
    18:12:40
    18:12:40 *** Listening on default...
Ravi
  • 2,472
  • 3
  • 20
  • 26
  • Not a complete answer, but a solid starting point is using virtualenv, and running Zope2 with your virtualenv Python. – sdupton Jun 10 '16 at 18:21
  • It already runs under virtualenv – Ravi Jun 10 '16 at 18:26
  • 1
    Then have you used ``pip`` to install rq, redis, etc? As long as your dependencies are in your python's site-packages, you should be fine to import them from non-restricted External method. – sdupton Jun 10 '16 at 22:42
  • Yes thats what I did using easy_install. Redis server is outside virtualenv but rq is part of virtualenv. – Ravi Jun 11 '16 at 17:57
  • 1
    Given your ImportError, you should be using a package distribution that is in your site-packages to house any code you want to run -- however it is actually run. Whatever ``def_update_company_status`` is (strange name for a package?), you need it in your sys.path. – sdupton Jun 12 '16 at 14:40
  • this works when executed from Python shell by removing the code related to python-rq. i.e. it imports def_update_company_status module. So, the problem seems to be with python-rq. sys.path does have the directory mentioned when printed – Ravi Jun 13 '16 at 11:42

0 Answers0