最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]用Flask-RQ2运行后台任务失败:ConnectionError Error 97 connecting to localhost 6379

Flask crifan 2807浏览 0评论

折腾:

[已解决]Flask中添加后台进程用于提醒到期时发送通知

期间,用代码:

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/sipevents/sipevents/__init__.py

from flask_rq2 import RQ
rq = RQ(app)

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/sipevents/sipevents/views.py

############################################################
# Flask-RQ2
############################################################
from . import rq
############################################################
# Flask-RQ2 related
############################################################
@rq.job
def add(x, y):
    gLog.debug(“x=%s, y=%x”, x, y)
    return x + y
@app.route(‘/creat_event’, methods=[‘GET’, ‘POST’])
@login_required
def creat_event():
        gLog.debug(“after create event, try use rq2 to run background work”)
        jobAdd = add.queue(1, 2)
        gLog.debug(“jobAdd=%s”, jobAdd)
        scheduledJobAdd = add.schedule(timedelta(seconds=20), 3, 4) # queue job in 20 seconds
        gLog.debug(“scheduledJobAdd=%s”, scheduledJobAdd)

结果出错:

after create event, try use rq2 to run background work

<div–<——————————————————————————

[2016-09-04 11:22:26 +0000] [19152] [ERROR] Error handling request /creat_event
Traceback (most recent call last):
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 135, in handle
    self.handle_request(listener, req, client, addr)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 176, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File “/root/html/SIPEvents/sipevents/views.py”, line 739, in decorated_function
    return f(*args, **kwargs)
  File “/root/html/SIPEvents/sipevents/views.py”, line 916, in creat_event
    jobAdd = add.queue(1, 2)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask_rq2/helpers.py”, line 55, in queue
    at_front=at_front,
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/rq/queue.py”, line 216, in enqueue_call
    job = self.enqueue_job(job, at_front=at_front)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/rq/queue.py”, line 284, in enqueue_job
    pipe.execute()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/redis/client.py”, line 2641, in execute
    return execute(conn, stack, raise_on_error)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/redis/client.py”, line 2495, in _execute_transaction
    connection.send_packed_command(all_cmds)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/redis/connection.py”, line 538, in send_packed_command
    self.connect()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/redis/connection.py”, line 442, in connect
    raise ConnectionError(self._error_message(e))
ConnectionError: Error 97 connecting to localhost:6379. Address family not supported by protocol.

好像是:

此处后台的redis没有运行,所以出现连接错误?

搜:

ConnectionError: Error 97 connecting to localhost:6379. Address family not supported by protocol.

Flask ConnectionError: Error 97 connecting to localhost:6379. Address family not supported by protocol.

Flask ConnectionError: Error 97 connecting to localhost:6379

socket.error: [Errno 97] Address family not supported by protocol · Issue #242 · liftoff/GateOne

resque – Rescue : Connection refused – Unable to connect to Redis on localhost:6379 – Stack Overflow

(SIPEvents) ➜  SIPEvents ps aux | grep redis
root     19236  0.0  0.0 103364   820 pts/1    S+   11:28   0:00 grep –color=auto –exclude-dir=.bzr –exclude-dir=CVS –exclude-dir=.git –exclude-dir=.hg –exclude-dir=.svn redis
(SIPEvents) ➜  SIPEvents

[整理]命令前面加上nohup是什么含义

去试试:

nohup redis-server >> redis.log 2>&1 &

结果竟然是:

虽然运行了,但是又退出了

期间生成了log文件:

(SIPEvents) ➜  SIPEvents nohup redis-server >> redis.log 2>&1 &
[1] 19594
(SIPEvents) ➜  SIPEvents 
[1]  + 19594 exit 1     nohup redis-server >> redis.log 2>&1
(SIPEvents) ➜  SIPEvents netstat -tupln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:10022               0.0.0.0:*                   LISTEN      17286/docker-proxy  
tcp        0      0 0.0.0.0:9000                0.0.0.0:*                   LISTEN      19706/docker-proxy  
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      17220/mysqld        
tcp        0      0 0.0.0.0:9003                0.0.0.0:*                   LISTEN      19669/docker-proxy  
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      19015/nginx         
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1335/sshd           
tcp        0      0 0.0.0.0:3000                0.0.0.0:*                   LISTEN      17240/docker-proxy  
tcp        0      0 0.0.0.0:8088                0.0.0.0:*                   LISTEN      29962/docker-proxy  
tcp        0      0 0.0.0.0:5432                0.0.0.0:*                   LISTEN      18421/docker-proxy  
tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      19505/docker-proxy  
tcp        0      0 0.0.0.0:32771               0.0.0.0:*                   LISTEN      8748/docker-proxy   
tcp        0      0 0.0.0.0:32772               0.0.0.0:*                   LISTEN      8782/docker-proxy   
udp        0      0 192.168.42.1:123            0.0.0.0:*                               1346/ntpd           
udp        0      0 115.29.173.126:123          0.0.0.0:*                               1346/ntpd           
udp        0      0 10.161.170.247:123          0.0.0.0:*                               1346/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               1346/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               1346/ntpd           
(SIPEvents) ➜  SIPEvents ll
total 56K
-rw-r–r– 1 root root  833 Sep  2 16:54 config.py
-rw-r–r– 1 root root  348 Sep  2 16:59 config.pyc
-rw-r–r– 1 root root 6.8K Aug 30 17:53 db_create.py
-rw-r–r– 1 root root  773 Aug 29 17:57 db_manager.py
-rw-r–r– 1 root root   96 Sep  3 22:25 dump.rdb
drwxr-xr-x 2 root root 4.0K Sep  4 11:22 instance
drwxr-xr-x 2 root root 4.0K Sep  2 16:39 logs
-rw-r–r– 1 root root  281 Sep  4 12:18 redis.log
-rw-r–r– 1 root root  431 Sep  2 15:56 requirements.txt
-rw-r–r– 1 root root   77 Aug 23 14:39 run.py
-rw-r–r– 1 root root  266 Aug 23 15:43 run.pyc
drwxr-xr-x 4 root root 4.0K Sep  4 11:06 sipevents
drwxr-xr-x 2 root root 4.0K Aug 21 11:04 toDel
(SIPEvents) ➜  SIPEvents cat redis.log 
nohup: ignoring input
19594:C 04 Sep 12:18:23.733 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
19594:M 04 Sep 12:18:23.736 # Creating Server TCP listening socket *:6379: unable to bind socket

忘了加上配置文件了。。。

(SIPEvents) ➜  SIPEvents nohup redis-server /etc/redis/redis.conf >> redis.log 2>&1 &
[1] 19659
(SIPEvents) ➜  SIPEvents netstat -tplun | grep redis
tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN      19659/redis-server

可见,已经正常运行了。

此刻的log是:

(SIPEvents) ➜  SIPEvents cat redis.log 
nohup: ignoring input
19594:C 04 Sep 12:18:23.733 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
19594:M 04 Sep 12:18:23.736 # Creating Server TCP listening socket *:6379: unable to bind socket
nohup: ignoring input
                _._                                                  
           _.-“__ ”-._                                             
      _.-“    `.  `_.  ”-._           Redis 3.2.3 (00000000/0) 64 bit
  .-“ .-“`.  “`\/    _.,_ ”-._                                   
 (    ‘      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-…-` __…-.“-._|’` _.-‘|     Port: 6379
 |    `-._   `._    /     _.-‘    |     PID: 19659
  `-._    `-._  `-./  _.-‘    _.-‘                                   
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io        
  `-._    `-._`-.__.-‘_.-‘    _.-‘                                   
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  
 |    `-._`-._        _.-‘_.-‘    |                                  
  `-._    `-._`-.__.-‘_.-‘    _.-‘                                   
      `-._    `-.__.-‘    _.-‘                                       
          `-._        _.-‘                                           
              `-.__.-‘                                               
19659:M 04 Sep 12:20:34.528 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
19659:M 04 Sep 12:20:34.529 # Server started, Redis version 3.2.3
19659:M 04 Sep 12:20:34.529 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.
19659:M 04 Sep 12:20:34.529 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled’ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
19659:M 04 Sep 12:20:34.529 * DB loaded from disk: 0.000 seconds
19659:M 04 Sep 12:20:34.529 * The server is now ready to accept connections on port 6379

然后就可以正常运行,至少没有报错了。

[总结]

此处,是由于后台没有运行redis而导致程序出错:

ConnectionError: Error 97 connecting to localhost:6379. Address family not supported by protocol

解决办法就是:

去让redis后台运行

命令是:

nohup redis-server /etc/redis/redis.conf >> redis.log 2>&1 &

说明:

1.确保此处环境变量中已经有了redis-server,所以可以直接使用

2.确保此处已经有对应的配置文件了:/etc/redis/redis.conf

3.nohup是让,即使当前终端断开,redis仍能在后台运行

4. >> redis.log,是stdout的标准输出,追加输出到对应的log文件redis.log中

5.2>&1,是让2==stderr==标准错误输出,重定向,和上面的stdout一样,也输出到上述log文件redis.log中。其中:&1表示文件描述符1==stdout

6.&,表示让redis在后台运行。

转载请注明:在路上 » [已解决]用Flask-RQ2运行后台任务失败:ConnectionError Error 97 connecting to localhost 6379

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.203 seconds, using 22.24MB memory