I just got it working using the global variable (the first of your suggestions). I did not know about Python's global statement, but that did the trick. I just had to say:
final_graph = None def graph_search(request): global final_graph if not final_graph: final_graph = build_graph() result = search_graph(final_graph, some_parameters_from_request) return render_to_response('template.html',{'result': result}) Thanks again, Dan! On Feb 13, 6:42 am, mickey ristroph <[EMAIL PROTECTED]> wrote: > Dan- > > Thanks a lot for the ideas. > > I tried the first suggestion, but get an UnboundLocalError, local > variable 'graph' referenced before assignment. Not sure how to fix > this. > > I also tried the following: > > local_data = threading.local() > local_data.__dict__.setdefault('graph', None) > if not local_data.graph: > local_data.graph = build_graph() > > But the graph is still rebuilt every time. This occurs through > mod_python and FastCGI. I guess there is a seperate thread handling > each request, so there is no persistence. > > I just wanted to give you an update on what I was trying. Thanks again > for the suggestions. I'll post again when I figure it out. > > -Mickey > > On Feb 1, 4:16 am, Dan Fairs <[EMAIL PROTECTED]> wrote: > > > > Is there anyway I can make g persistent? In other words, I want to > > > build it once, and then search that copy for each additional request. > > > If this thing doesn't change, then you could build it once and then > > stick it in a module-level variable. Something like: > > > final_graph = None > > > def graph_search(request): > > if not final_graph: > > final_graph = build_graph() > > result = search_graph(final_graph, some_parameters_from_request) > > return render_to_response('template.html',{'result': result}) > > > Note that this is a *very* naive implementation - you'll need some > > locking around final_graph access to prevent race conditions. You > > might want to consider using a thread-local storage. You'll also need > > to unset this module-level variable (again with locking) if your > > underlying graph data changes. > > > Cheers, > > Dan > > > -- > > Dan Fairs <[EMAIL PROTECTED]> |http://www.stereoplex.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---