用 flask-socketio 实现了一个 socketio 的后台接口,由于 flask-socketio 依赖 eventlet,所以又安装了 eventlet。
然后发现其在 docker 内使用时,无法解析 docker compose 中配置的容器别名。
折腾了半天,最重要的收获是,尽量不要用 python eventlet 这些库,就跟玩具一样,完全浪费生命。早换 golang 早享受。
DNS Lookup timed out
redis.exceptions.ConnectionError: Error -3 connecting to redis:6379. Lookup timed out.
File "/usr/local/lib/python3.11/site-packages/eventlet/support/greendns.py", line 463, in resolve
return _proxy.query(name, rdtype, raise_on_no_answer=raises,
File "/usr/local/lib/python3.11/site-packages/eventlet/support/greendns.py", line 419, in query
return end()
File "/usr/local/lib/python3.11/site-packages/eventlet/support/greendns.py", line 398, in end
raise result[1]
File "/usr/local/lib/python3.11/site-packages/dns/resolver.py", line 1076, in _compute_timeout
raise LifetimeTimeout(timeout=duration, errors=errors)
File "/usr/local/lib/python3.11/site-packages/redis/connection.py", line 1461, in get_connection
connection.connect()
File "/usr/local/lib/python3.11/site-packages/redis/connection.py", line 713, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error -3 connecting to redis:6379. Lookup timed out.
参考
- https://github.com/eventlet/eventlet/issues/619
- https://medium.com/@pablankley/the-eventlet-dns-bug-that-could-take-down-your-production-servers-fa873253bb40
找到两个类似的问题
Any progress on this? This took me many hours, too. Now I am just using dnspython==1.16.0 for a temporary workaround.
解决方案就是降级 dnspython 的版本。
查看当前环境中的 dnspython 版本
在 conda 环境内:
> pip list | grep dns
dnspython 2.6.0
降级 dnspython 版本
> pip install dnspython==1.16.0
Successfully uninstalled dnspython-2.7.0
Successfully installed dnspython-1.16.0
降级 python 版本
单纯降级 dnspython 版本,还不行。在 python 3.11 下,会报错:
AttributeError: module 'collections' has no attribute 'MutableMapping'
github issue 中也提到了这个问题:
As a heads-up, it looks like supporting newer dnspython will be a necessary prerequisite to supporting Python 3.10. dnspython<2.0.0 has a reference to collections.MutableMapping, which was deprecated in favor of collections.abc.MutableMapping in py33 and removed in py310.
所以,将 docker python 镜像的版本降级为 3.9。就正常了。
确认 redis 中已成功写入数据
> docker exec -it redis /bin/sh
/data # redis-cli
127.0.0.1:6379> keys *
1) "name"
微信关注我哦 👍
我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式