2

I would like to have a computational simulation running on a background process (started with redis rq) where I can query its current state, as well as change parameters using Django.

For the sake of simplicity: let's say I want to run the following code for a long time (which I would set up through a python worker):

def simulation(a=1):
     value = 0
     while a != None:     
          value += a
          time.sleep(5)

Then, by visiting a URL, it would tell me the current value of value. I could also POST to a URL to change the value of a i.e. a=None to stop the simulation or a=-10 to change the behavior.

What is the best way to do this?

Ben
  • 6,986
  • 6
  • 44
  • 71
  • To me this sounds like a bit of a primarily opinion based question, but I would suggest that if you are already familiar with python-rq to go with it. you could probably do this with pure redis-py as well. – e4c5 Apr 19 '16 at 01:15
  • Hi @e4c5, you're right it did start that way. Just wondering if there was some technique that people used that I didn't know of. It seems like my attempts have failed however, are you aware of any other ways to accomplish this? – Ben Apr 20 '16 at 16:34
  • I've solved this problem more than once by using redis.lpush() and redis.rpop() simplest way that I know of – e4c5 Apr 20 '16 at 23:45
  • Nevermind, you're right, cache works fine. I was just being a dunce. – Ben Apr 21 '16 at 00:38

1 Answers1

0

This best way I've found to do this is using cache

from django.core.cache import cache

def simulation(a=1):
     value = 0
     while a != None:     
          value += a
          cache.set('value', value, 3600)
          time.sleep(5)
          a = cache.get('a', None)

This does work, but it's quite slow for my needs. Perhaps there's a method using sockets, but I wasn't abe to get it to work. The socket is blocked in the background process.

Ben
  • 6,986
  • 6
  • 44
  • 71