Created
December 28, 2010 22:12
-
-
Save martinsam/757803 to your computer and use it in GitHub Desktop.
CACHE - Django signals with timeout / process
This file contains hidden or 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
| # -*- coding: utf-8 -*- | |
| import os | |
| import time | |
| from multiprocessing import Process | |
| from django.template.loader import render_to_string | |
| from django.http import HttpRequest | |
| from django.template import RequestContext | |
| from django.contrib.auth.models import User | |
| def project_post_generate_template(sender, **kwargs): | |
| # 1. check / create an new entrie in dict | |
| # 2. launch new subprocess | |
| # 3. kill old process | |
| # 4. stock process in dict | |
| ### global var ### | |
| global nb_call | |
| global process_tab | |
| ### get object ### | |
| project = kwargs['instance'] | |
| ### nb call ### | |
| nb_call +=1 | |
| print nb_call | |
| ### launch new process | |
| p=Process(target=generate_project_cartridge,kwargs={'project' : project, 'timeout': 3}) | |
| p.start() | |
| if str(project.id) in process_tab: | |
| ### kill old process | |
| process_tab[str(project.id)].terminate() | |
| else: | |
| process_tab[str(project.id)] = p | |
| print process_tab | |
| #print p.terminate() | |
| def generate_project_cartridge(project, timeout = 0): | |
| if time: | |
| time.sleep(timeout) | |
| request = HttpRequest() | |
| for coworker in project.get_coworkers(): | |
| request.user = coworker | |
| c = render_to_string('mon/projects/cartridges/cache.html', {"project": project}, RequestContext(request)).encode("utf-8") | |
| f = open(os.path.join(settings.CACHE_TEMPLATE_PATH, 'projects/dashboard/cache_%s_%s.html' % (project.id, request.user.id)), 'w') | |
| f.write(c) | |
| f.close() | |
| print "Generate project cartridge done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment