如何修复Flask RQ Worker中的无模块错误

时间:2019-05-16 07:47:18

标签: python-3.x flask redis docker-compose

我正在使用docker-compose与Redis和一个工作程序一起构建Web应用程序。此外,我还有要监视的RQ仪表板。

整个项目都在无所事事的Ubuntu环境中

项目结构:

project/
       app_main/
                moduleA/
                        classA  - The task to do
                jobs.py    - Implements the job
                queue.py   - Has the code perform.queue(id)
      scripts/
              worker/
                     dockerfile
                     worker.py
      dockerfile
      docker-compose.yml
      app.py

泊坞窗正确构建,该应用程序正常运行。但是当工作程序运行时,会出现错误:ModuleNotFoundError:没有名为“ app_main”的模块

我在docker-compose中添加了--path,而不是拥有一个单独的dockerfile,仍然出现了错误。


# jobs.py
from flask_rq2 import RQ
from app_main.moduleA.classA import methodA

rq = RQ(redis_url="redis://redis:6379/0")

@rq.job
def perform(id):

    methodA().subMethodA(id)

    return True


# worker.py

import os

import redis
from rq import Worker, Queue, Connection

listen = ['high', 'default', 'low']

redis_url = os.getenv('REDISTOGO_URL', 'redis://redis:6379')

conn = redis.from_url(redis_url)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()

docker-compose


version: "3.7"

services:

  web:
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    ports:
      - "5000:5000"
    depends_on:
      - redis
    restart: always
    volumes:
      - .:/app
    image: web
    environment:
      - MODE=dev
      - REDIS_URL='redis://redis:6379/0'
      - RQ_REDIS_URL='redis://redis:6379/0'
    networks:
      - app-net

  redis:
    image: redis:4.0.11-alpine
    command: ["redis-server", "--appendonly", "yes"]
    hostname: redis
    networks:
      - app-net
    volumes:
      - redis-data:/data


  dashboard:
    build: ./app_main/dashboard
    image: dashboard
    container_name: dashboard
    ports:
      - '9181:9181'
      - '6379:6379'
    command: rq-dashboard -H redis
    networks:
      - app-net
    volumes:
      - redis-data:/data

  job_worker:
    build: ./scripts/worker
    depends_on:
      - redis
    networks:
      - app-net
    volumes:
      - redis-data:/data

# Docker network
networks:
  app-net:

volumes:
  redis-data:

错误日志

[31mjob_worker_1    |[0m Traceback (most recent call last):
[31mjob_worker_1    |[0m   File "/usr/local/lib/python3.7/site-packages/rq/worker.py", line 812, in perform_job
[31mjob_worker_1    |[0m     rv = job.perform()
[31mjob_worker_1    |[0m   File "/usr/local/lib/python3.7/site-packages/rq/job.py", line 588, in perform
[31mjob_worker_1    |[0m     self._result = self._execute()
[31mjob_worker_1    |[0m   File "/usr/local/lib/python3.7/site-packages/rq/job.py", line 594, in _execute
[31mjob_worker_1    |[0m     return self.func(*self.args, **self.kwargs)
[31mjob_worker_1    |[0m   File "/usr/local/lib/python3.7/site-packages/rq/job.py", line 199, in func
[31mjob_worker_1    |[0m     return import_attribute(self.func_name)
[31mjob_worker_1    |[0m   File "/usr/local/lib/python3.7/site-packages/rq/utils.py", line 155, in import_attribute
[31mjob_worker_1    |[0m     module = importlib.import_module(module_name)
[31mjob_worker_1    |[0m   File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
[31mjob_worker_1    |[0m     return _bootstrap._gcd_import(name[level:], package, level)
[31mjob_worker_1    |[0m   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[31mjob_worker_1    |[0m   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[31mjob_worker_1    |[0m   File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[31mjob_worker_1    |[0m   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[31mjob_worker_1    |[0m   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[31mjob_worker_1    |[0m   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[31mjob_worker_1    |[0m   File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
[31mjob_worker_1    |[0m ModuleNotFoundError: No module named 'app_main'

工人dockerfile

FROM python:3

ADD ./worker.py .

RUN pip install rq redis

ENTRYPOINT ["python"]

CMD ["./worker.py"]

1 个答案:

答案 0 :(得分:0)

我尝试了许多解决方案,但是没有用。

我所做的解决方案是,将worker dockerfile放置到根文件夹中。使用WORKDIR ./app修改了dockerfile并从pip install -r requirements.txt安装了依赖项。