Search code examples
pythonvariablesservicedaemon

How to section a part of program run again and again as a service?


Following is the structure of my code:

Part 1:

var result =  <-- a very resource heavy calculation-->;

Part 2:

var a = result.x;
var b   = result.y;

print a;
print b;     

Now, I want to build a program that allows me to have variable result available (i.e. Part 1) all the time and to run the "Part 2" again and again. So it will be like a program in a running state which has pre-calculated variables and uses it again and again.


Solution

  • This looks like a producer-consumer problem.

    Some guy talking about that, in Python: http://agiliq.com/blog/2013/10/producer-consumer-problem-in-python/

    Basically, make a producer thread that generates result, then have a consumer thread that reads it. Ensure that access to the result variable is synchronized / serialized. Run both threads simultaneously, to taste.