为什么我的芹菜任务仅同步运行?

时间:2019-04-24 21:27:43

标签: python django-celery django-2.2 celery-4

我正在使用Django,Celery,Gunicorn和Nginx建立一个网站。我的渲染任务需要很长时间才能处理,因此我一直试图将其放入Celery任务中。问题在于Celery似乎只能同步运行任务。

我遵循了Celery网站上的“第一步”,“ Django第一步”,“下一步”和“守护进程(使用systemd)”教程。首先,我在task.py下创建了一个shared_task,并在其中调用了我的函数。然后,我将该函数复制到task.py。我尝试将其更改为另一种任务。我还确保CELERY_TASKS_ALWAYS_EAGER为假。该函数肯定会被调用并运行,由于某种原因,直到完成,一切都不会发生。

这是我的任务。py:

from __future__ import absolute_import, unicode_literals
from celery import shared_task
import sqlite3
import matplotlib.pyplot as plt
import ast
import os
from util import CNN

@shared_task
def render_images(uid, outdir):
    conn = sqlite3.connect('../Bat_Echolocation_2019/db.sqlite3')
    c = conn.cursor()

    if 'status' not in get_tables():
        query = 'CREATE TABLE status (uid INTEGER PRIMARY KEY, status BIT default "FALSE")'
        c.execute(query)

    c.execute('INSERT OR REPLACE INTO status VALUES (?, ?);', (uid, False,))

    # Load users image data
    c.execute('SELECT * FROM pulses WHERE uid=?;', (uid,))
    table = c.fetchall()
    table = [t for t in table if t[4] == uid]

    # Load CNN
    __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
    model = load_model(__location__ + '/CNN200x300ep30.h5')

    fig, ax = plt.subplots()

    for i, row in enumerate(table):
        # Only render files that haven't been rendered yet
        if row[2] == '1':
            continue

        # Mark pulse as rendered
        with conn:
            c.execute(f"UPDATE pulses SET classification = '1' WHERE {str(i)} = ROWID;")

        # Convert DB string to 2d list
        pulse = ast.literal_eval(row[1])

        # Split pulse into x and y coords
        x = [point[0] for point in pulse]
        y = [point[1] for point in pulse]

        # Generate image path
        save_path = f"{outdir}/^_{row[0].replace('#', '')}_{i}.png"

        # Render the image
        ax.axis('off')
        ax.scatter(x, y)
        fig.savefig(save_path, format='png', Transparency=True, dpi=50)
        plt.cla()
        gc.collect()
        # Classify as normal or abnormal
        # TODO- perform at insert?
        if not CNN.classifyCNN(save_path, model) == 0:
            os.rename(save_path, save_path.replace('^', 'e'))
        else:
            os.rename(save_path, save_path.replace('^', 'a'))

    c.execute(f'INSERT OR REPLACE INTO status VALUES ({uid}, {True});')
'''

celery.py
```python
from __future__ import absolute_import, unicode_literals
from celery import Celery
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'batalog.settings')
app = Celery('batalog', broker='amqp://localhost', include = ['batalog.tasks'])

app.config_from_object('django.conf:settings', namespace='CELERY')

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))
调用的

views.py函数

def render_pulses(request):
    # Get request data
    file_name = request.FILES['document'].name
    file = request.FILES['document'].read()
    uid = request.user.id

    # Upload file to database. Switches on filetype.
    # TODO- extend list of acceptable/unacceptable filetypes

    # Upload ZIP containing ZC files
    if file_name.endswith('.zip'):
        outdir = os.path.join(os.getcwd(), 'media', str(uid), 'zip_results')
        try:
            os.makedirs(outdir)
        except:
            pass
        db_API.insert_zip(uid, outdir, file_name, file)

    # Upload ZC file
    else:
        db_API.insert_pulse(uid, file_name, file)

    # Make output directory if it doesn't exist already
    outdir = os.path.join(os.getcwd(), 'media', str(uid), 'test_images')
    try:
        os.makedirs(outdir)
    except:
        pass

    # Render images to local storage
    render_images.delay(uid, outdir)

    return render(request, 'gallery.html')

settings.py


    import os
    #import posixpath

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = '8ace3072-47a0-4910-b522-dc3601f38c35'

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True

    ALLOWED_HOSTS = ['www.echobatalog.com', 'echobatalog.com', '0.0.0.0', '127.0.0.1', 'localhost']
    INTERNAL_IPS = ('0.0.0.0', '127.0.0.1', 'localhost',)

    INSTALLED_APPS = [
        'app',
        'material',
        'material.admin',
        'imagekit',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.sitemaps',
        'django.contrib.sites',
        'django_celery_results',
    ]

    SITE_ID = 1

    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'whitenoise.middleware.WhiteNoiseMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware'
    ]

    ROOT_URLCONF = 'batalog.urls'

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages'
                ],
            },
        },
    ]

    WSGI_APPLICATION = 'batalog.wsgi.application'

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

    AUTH_PASSWORD_VALIDATORS = [
        {
            'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
        },
    ]

    LOGIN_REDIRECT_URL = '/admin/'

    # Internationalization

    LANGUAGE_CODE = 'en-us'

    TIME_ZONE = 'UTC'

    USE_I18N = False

    USE_L10N = True

    USE_TZ = True

    # Static files (CSS, JavaScript, Images)
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    # kkeomalaythong edit 2019-04-14: original line causes error, so line was edited
    #STATIC_URL = os.path.join(BASE_DIR, 'static')
    STATIC_URL = os.path.join(BASE_DIR, 'static/')
    # end kkeomalaythong edit 2019-04-14

    # https://docs.djangoproject.com/en/1.8/howto/static-files/deployment/
    # python manage.py collectstatic
    # STATIC_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + ['static/']))
    # STATIC_ROOT = BASE_DIR + '/static/'
    STATICFILES_DIR = [
        os.path.join(BASE_DIR, 'static')
    ]

    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
    # MEDIA_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + ['media/']))
    # MEDIA_ROOT = BASE_DIR + '/media/'

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'handlers': {
            'file': {
                'level': 'DEBUG',
                'class': 'logging.FileHandler',
                'filename': BASE_DIR + '/debug.log',
            },
        },
        'loggers': {
            'django': {
                'handlers': ['file'],
                'level': 'DEBUG',
                'propagate': True,
            },
        },
    }


    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'unique-snowflake',
        }
    }



    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder'
    )
    LOGIN_REDIRECT_URL = '/'
    LOGOUT_REDIRECT_URL = '/'

    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_HOST = 'smtp.gmail.com'
    EMAIL_PORT = 587
    EMAIL_USE_TLS = True
    EMAIL_HOST_USER = 'aasdsa203@gmail.com'
    EMAIL_HOST_PASSWORD = 'hitman007'

    CELERY_RESULT_BACKEND = 'django-db'
    CELERY_CACHE_BACKEND = 'django-cache'

我希望render函数被调用,然后立即渲染'gallery'页面。但是,它将调用该函数并等待其完成以进入图库。

0 个答案:

没有答案