RaPo Spectre
RaPo Spectre

Reputation: 51

Celery could not start worker processes when using scrapy-Djangoitem

Question detail: https://github.com/celery/celery/issues/3598

I want to run a scrapy spider with celery, which contain Djangoitems.

this is my celery task:

# coding_task.py
import sys

from celery import Celery
from collector.collector.crawl_agent import crawl

app = Celery('coding.net', backend='redis', broker='redis://localhost:6379/0')
app.config_from_object('celery_config')


@app.task
def period_task():
    crawl()

collector.collector.crawl_agent.crawl contains a scrapy crawler who uses djangoitem as item. the item like:

import django

os.environ['DJANGO_SETTINGS_MODULE'] = 'RaPo3.settings'
django.setup()

from scrapy_djangoitem import DjangoItem
from xxx.models import Collection


class CodingItem(DjangoItem):
    django_model = Collection
    amount = scrapy.Field(default=0)
    role = scrapy.Field()
    type = scrapy.Field()
    duration = scrapy.Field()
    detail = scrapy.Field()
    extra = scrapy.Field()

when run: celery -A coding_task worker --loglevel=info --concurrency=1, it wil get some errors below:

[2016-11-16 17:33:41,934: ERROR/Worker-1] Process Worker-1
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/billiard/process.py", line 292, in _bootstrap
    self.run()
  File "/usr/local/lib/python2.7/site-packages/billiard/pool.py", line 292, in run
    self.after_fork()
  File "/usr/local/lib/python2.7/site-packages/billiard/pool.py", line 395, in after_fork
    self.initializer(*self.initargs)
  File "/usr/local/lib/python2.7/site-packages/celery/concurrency/prefork.py", line 80, in process_initializer
    signals.worker_process_init.send(sender=None)
  File "/usr/local/lib/python2.7/site-packages/celery/utils/dispatch/signal.py", line 151, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/usr/local/lib/python2.7/site-packages/celery/fixups/django.py", line 152, in on_worker_process_init
    self._close_database()
  File "/usr/local/lib/python2.7/site-packages/celery/fixups/django.py", line 181, in _close_database
    funs = [self._db.close_connection]  # pre multidb
AttributeError: 'module' object has no attribute 'close_connection'
[2016-11-16 17:33:41,942: INFO/MainProcess] Connected to redis://localhost:6379/0
[2016-11-16 17:33:41,957: INFO/MainProcess] mingle: searching for neighbors
[2016-11-16 17:33:42,962: INFO/MainProcess] mingle: all alone
/usr/local/lib/python2.7/site-packages/celery/fixups/django.py:199: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
  warnings.warn('Using settings.DEBUG leads to a memory leak, never '

[2016-11-16 17:33:42,968: WARNING/MainProcess] /usr/local/lib/python2.7/site-packages/celery/fixups/django.py:199: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
  warnings.warn('Using settings.DEBUG leads to a memory leak, never '

[2016-11-16 17:33:42,968: WARNING/MainProcess] [email protected] ready.
[2016-11-16 17:33:42,969: ERROR/MainProcess] Process 'Worker-1' pid:2777 exited with 'exitcode 1'
[2016-11-16 17:33:42,991: ERROR/MainProcess] Unrecoverable error: WorkerLostError('Could not start worker processes',)
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/celery/worker/__init__.py", line 208, in start
    self.blueprint.start(self)
  File "/usr/local/lib/python2.7/site-packages/celery/bootsteps.py", line 127, in start
    step.start(parent)
  File "/usr/local/lib/python2.7/site-packages/celery/bootsteps.py", line 378, in start
    return self.obj.start()
  File "/usr/local/lib/python2.7/site-packages/celery/worker/consumer.py", line 271, in start
    blueprint.start(self)
  File "/usr/local/lib/python2.7/site-packages/celery/bootsteps.py", line 127, in start
    step.start(parent)
  File "/usr/local/lib/python2.7/site-packages/celery/worker/consumer.py", line 766, in start
    c.loop(*c.loop_args())
  File "/usr/local/lib/python2.7/site-packages/celery/worker/loops.py", line 50, in asynloop
    raise WorkerLostError('Could not start worker processes')
WorkerLostError: Could not start worker processes

if i delete djangoitem in item:

from scrapy.item import Item
class CodingItem(item):
    amount = scrapy.Field(default=0)
    role = scrapy.Field()
    type = scrapy.Field()
    duration = scrapy.Field()
    detail = scrapy.Field()
    extra = scrapy.Field()

the task will play well and doesn't have any error. What should i do if i want to use djangoitem in this celery-scrapy task?

Thanks!

Upvotes: 2

Views: 2621

Answers (2)

shikhar srivastava
shikhar srivastava

Reputation: 33

You should check the ram usage. It might be possible celery is not getting enough ram

Upvotes: 1

RaPo Spectre
RaPo Spectre

Reputation: 51

Upgrade Celery to 4.0 will solve the problem.

More detail: https://github.com/celery/celery/issues/3598

Upvotes: 0

Related Questions