realpython / realpython-blog

real python blog posts
https://realpython.com/blog/
200 stars 86 forks source link

How to start redis from with flask app.py? #9

Closed scheung38 closed 9 years ago

scheung38 commented 9 years ago

Instead of launching redis from command line with :

sudo service redis_6379 start

how to do this within Python, thanks.

mjhea0 commented 9 years ago

You could do something as easy as-

import os
os.system("service redis_6379 start'")

but I think a fabfile would be the way to go. Fabric

scheung38 commented 9 years ago

Thanks Michael, if this is such an important task, not sure why most tutorials tell user to manually start in command line instead of doing this before every session?

mjhea0 commented 9 years ago

Because it's a a process that should already running. Think of when you connect to postgres. You don't manually start the server for each session. Instead, you run it in the background. If I were you, I would simply set up a try/except block to test for the connection first, rather than writing a script to open the connection itself.

scheung38 commented 9 years ago

But it is to be deployed to AWS, so it may probably not be already running. I would have assumed that we need to launch it if it is not already running in the background?

import os
os.system("service redis_6379 restart")

returns

*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 104
>>> 'logfile /var/log/redis_6379.log'
Can't open the log file: Permission denied
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
/var/run/redis_6379.pid does not exist, process is not running
Starting Redis server...

*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 104
>>> 'logfile /var/log/redis_6379.log'
Can't open the log file: Permission denied

and

import os
os.system("sudo service redis_6379 restart")

returns

sudo: no tty present and no askpass program specified
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
sudo: no tty present and no askpass program specified
mjhea0 commented 9 years ago

http://stackoverflow.com/a/24107529/1799408

scheung38 commented 9 years ago

So is it straight forward to have redis (mongod or any other database servers) running on AWS or Heroku?

scheung38 commented 9 years ago

So AWS can easily deploy redis? if so how would you define that thanks

mjhea0 commented 9 years ago

https://aws.amazon.com/elasticache/

nirorman commented 5 years ago

https://stackoverflow.com/questions/24374490/amazon-elasticcache-redis-public-access#answer-25913518 You can't access AWS Elastic Cache from outside aws, so you can't use that redis when developing locally.