0

I want to use multiprocessing to do the parallel computing. But I find if I use multiprocessing, the code should contains

if __name__ == '__main__':

or I'll get error

PicklingError: Can't pickle <type 'function'>: attribute lookup__builtin__.function failed

How can I do? Thank you.

CrackyCat
  • 43
  • 1
  • 5

2 Answers2

0

Where are you trying to use multiproccesing in django?

Django handles requests in a synchronous manner, so generally most tasks carried out when servicing requests should not be processor intensive, as this would cause to large a delay before a response can be returned to the user.

Instead using something like celery: http://www.celeryproject.org/ to carry out background tasks is usually the preferred methods, a blog entry about this: http://www.turnkeylinux.org/blog/django-celery-rabbitmq.

Henry Florence
  • 2,848
  • 19
  • 16
  • Also, Django might handle things internally synchronously, but the web server could spawn multiple Django daemons to handle different requests. – Mike DeSimone Jun 26 '14 at 03:54
0

Actually, the if __name__ == '__main__': is needed as described in the document: https://docs.python.org/2/library/multiprocessing.html#multiprocessing.sharedctypes.Array.

in django, the multiprocessing building also can be placed in the function 'handle' just as:

class Command(BaseCommand):

  def handle(self, *args, **options):
    processes = range(PROCESS_NUM)
    for i in range(PROCESS_NUM):
      processes[i] = Process(target=yourFunc, args=(...))
      processes[i].start()
Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
Yx.Ac
  • 111
  • 2
  • 5