Created
February 11, 2014 13:46
-
-
Save rikva/8935098 to your computer and use it in GitHub Desktop.
Python multiprocessing example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing | |
import logging | |
import subprocess | |
from django.core.management.base import BaseCommand | |
import time | |
from products.models import ProductFeed | |
log = logging.getLogger('xxx.fetcher') | |
log.info('Starting feed parser') | |
logdir = '/var/log/xxx' | |
def handle_feed(feed): | |
logfile = open(logdir + "/feed" + str(feed.id), "w") | |
log.info('Start parsing feed: %s, %s; %s' % (feed.id, feed.affiliate, feed.identifier)) | |
subprocess.call(('python', 'manage.py', 'parse_feed', str(feed.id)), stdout=logfile, stderr=logfile) | |
log.info('Done parsing feed: %s, %s; %s' % (feed.id, feed.affiliate, feed.identifier)) | |
logfile.close() | |
class Command(BaseCommand): | |
def handle(self, *args, **options): | |
if args and args[0].isdigit(): | |
max_processes = int(args[0]) | |
else: | |
max_processes = 5 | |
print 'Starting with %s workers' % max_processes | |
pool = multiprocessing.Pool(max_processes) | |
# if len(args) > 1: | |
# feed_ids = args[1].split(',') | |
# feeds = [] | |
# for id in feed_ids: | |
# feeds += ProductFeed.objects.filter(is_active=True, id=id) | |
# result = pool.map_async(handle_feed, feeds) | |
# else: | |
if 'order_asc' in args: | |
print 'Starting in ascending order' | |
result = pool.map_async(handle_feed, ProductFeed.objects.filter(is_active=True).order_by('id')) | |
else: | |
print 'Starting in descending order' | |
result = pool.map_async(handle_feed, ProductFeed.objects.filter(is_active=True).order_by('-id')) | |
result.wait() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment