通常每个套接字地址(协议/网络地址/端口)只允许使用一次。 数据库连接不释放测试 连接池 释放连接 关闭连接 有关 redis-py 连接池会导致服务器产生大量 CLOSE_WAIT 的再讨论以及一个解决方案
import pymysql
from redis import Redis
import time
h, pt, u, p, db = '192.168.2.210', 3306, 'root', 'nfwt&2016', 'xl_product_DONOT_REMOVE'
h, pt, u, p, db = '192.168.2.130', 3306, 'root', 'root', 'xl_product_DONOT_REMOVE'
def mysql_fetch(sql, res_type='tuple'):
global h, pt, u, p, db
try:
conn = pymysql.connect(host=h, port=pt, user=u, passwd=p, db=db, charset='utf8')
except Exception as e:
print(e)
return ()
if res_type == 'dic':
cursor = conn.cursor(pymysql.cursors.DictCursor)
else:
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
return cursor.fetchall()
def mysql_write(sql):
global h, pt, u, p, db
try:
conn = pymysql.connect(host=h, port=pt, user=u, passwd=p, db=db, charset='utf8')
except Exception as e:
print(e)
return 1
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
return 0
REDIS_HOST, REDIS_PORT, PASSWORD = '192.168.2.51', '6379', 'mypwd'
rds = Redis(host=REDIS_HOST, port=REDIS_PORT, password=PASSWORD)
c = 0
while True:
# if 3>1:
s_404 = [ii.decode('utf-8') for ii in rds.smembers('chk_url_404')]
for i in s_404:
c += 1
if c % 2 == 1:
continue
sql = 'SELECT id FROM chk_url_404 WHERE url="{}" '.format(i)
try:
r = mysql_fetch(sql)
print(sql)
except Exception as e:
print("select----", e)
continue
if len(r) == 0:
# sql = 'INSERT INTO chk_url_404 (url,indb_time) VALUES ("{}","{}")'.format(i, str(int(time.time())))
# sql = 'INSERT INTO chk_url_404 (url) VALUES ("{}")'.format(i)
sql = 'INSERT INTO chk_url_404 (url,indb_time) VALUES ("%s","%s")' % (i, int(time.time()))
print(sql)
try:
mysql_write(sql)
print("ori----", sql)
except Exception as e:
print("insert--exe----", sql, e)
continue
time.sleep(60 * 15)
同时,开2个相同脚本运行
SELECT id FROM chk_url_404 WHERE url="www.ailaba.org/sell/1077035.html"
INSERT INTO chk_url_404 (url,indb_time) VALUES ("www.ailaba.org/sell/1077035.html","1520575290")
(2003, "Can't connect to MySQL server on '192.168.2.130' ([WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。)")
ori---- INSERT INTO chk_url_404 (url,indb_time) VALUES ("www.ailaba.org/sell/1077035.html","1520575290")
(2003, "Can't connect to MySQL server on '192.168.2.130' ([WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。)")
SELECT id FROM chk_url_404 WHERE url="www.ailaba.org/sell/176458743032220.html"
INSERT INTO chk_url_404 (url,indb_time) VALUES ("www.ailaba.org/sell/176458743032220.html","1520575290")
(2003, "Can't connect to MySQL server on '192.168.2.130' ([WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。)")
ori---- INSERT INTO chk_url_404 (url,indb_time) VALUES ("www.ailaba.org/sell/176458743032220.html","1520575290")
(2003, "Can't connect to MySQL server on '192.168.2.130' ([WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。)")
SELECT id FROM chk_url_404 WHERE url="site.leshou.com/s/4881015.html"
INSERT INTO chk_url_404 (url,indb_time) VALUES ("site.leshou.com/s/4881015.html","1520575290")
(2003, "Can't connect to MySQL server on '192.168.2.130' ([WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。)")
ori---- INSERT INTO chk_url_404 (url,indb_time) VALUES ("site.leshou.com/s/4881015.html","1520575290")
(2003, "Can't connect to MySQL server on '192.168.2.130' ([WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。)")
SELECT id FROM chk_url_404 WHERE url="www.ailaba.org/sell/3559346.html"
INSERT INTO chk_url_404 (url,indb_time) VALUES ("www.ailaba.org/sell/3559346.html","1520575290")
(2003, "Can't connect to MySQL server on '192.168.2.130' ([WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。)")
win
netstat -ano
TCP 192.168.3.99:56629 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56630 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56631 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56632 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56633 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56634 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56635 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56636 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56637 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56638 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56639 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56640 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56641 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56642 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56643 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56644 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56645 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56646 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56647 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56648 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56649 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56650 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56651 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56652 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56653 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56654 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56655 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56656 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56657 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56658 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56659 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56660 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56661 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56662 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56663 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56664 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56665 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56666 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56667 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56668 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56669 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56670 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56671 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56672 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56673 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56674 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56675 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56676 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56677 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56678 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56679 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56680 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56681 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56682 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56683 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56684 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56685 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56686 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56687 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56688 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56689 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56690 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56691 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56692 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56693 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56694 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56695 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56696 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56697 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56698 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56699 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56700 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56701 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56702 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56703 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56704 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56705 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56706 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56707 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56708 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56709 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56710 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56711 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56712 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56713 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56714 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56715 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56716 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56717 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56718 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56719 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56720 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56721 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56722 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56723 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56724 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56725 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56726 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56727 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56728 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56729 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56730 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56731 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56732 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56733 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56734 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56735 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56736 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56737 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56738 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56739 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56740 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56741 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56742 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56743 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56744 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56745 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56746 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56747 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56748 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56749 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56750 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56751 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56752 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56753 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56754 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56755 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56756 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56757 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56758 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56759 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56760 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56761 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56762 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56763 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56764 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56765 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56766 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56767 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56768 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56769 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56770 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56771 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56772 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56773 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56774 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56775 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56776 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56777 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56778 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56779 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56780 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56781 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56782 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56783 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56784 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56785 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56786 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56787 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56788 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56789 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56790 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56791 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56792 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56793 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56794 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56795 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56796 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56797 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56798 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56799 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56800 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56801 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56802 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56803 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56804 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56805 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56806 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56807 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56808 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56809 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56810 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56811 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56812 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56813 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56814 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56815 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56816 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56817 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56818 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56819 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56820 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56821 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56822 34.192.119.70:443 ESTABLISHED 5104 TCP 192.168.3.99:56823 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56824 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56825 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56826 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56827 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56828 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56829 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56830 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56831 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56832 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56833 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56834 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56835 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56836 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56837 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56838 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56839 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56840 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56841 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56842 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56843 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56844 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56845 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56846 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56847 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56848 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56849 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56850 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56851 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56852 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56853 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56854 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56855 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56856 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56857 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56858 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56859 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56860 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56861 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56862 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56863 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56864 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56865 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56866 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56867 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56868 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56869 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56870 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56871 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56872 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56873 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56874 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56875 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56876 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56877 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56878 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56879 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56880 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56881 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56882 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56883 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56884 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56885 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56886 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56887 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56888 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56889 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56890 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56891 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56892 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56893 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56894 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56895 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56896 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56897 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56898 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56899 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56900 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56901 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56902 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56903 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56904 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56905 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56906 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56907 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56908 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56909 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56910 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56911 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56912 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56913 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56914 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56915 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56916 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56917 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56918 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56919 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56920 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56921 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56922 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56923 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56924 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56925 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56926 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56927 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56928 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56929 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56930 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56931 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56932 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56933 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56934 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56935 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56936 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56937 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56938 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56939 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56940 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56941 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56942 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56943 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56944 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56945 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56946 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56947 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56948 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56949 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56950 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56951 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56952 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56953 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56954 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56955 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56956 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56957 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56958 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56959 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56960 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56961 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56962 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56963 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56964 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56965 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56966 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56967 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56968 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56969 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56970 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56971 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56972 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56973 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56974 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56975 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56976 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56977 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56978 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56979 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56980 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56981 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56982 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56983 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56984 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56985 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56986 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56987 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56988 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56989 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56990 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56991 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56992 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56993 192.168.3.212:6379 TIME_WAIT 0 TCP 192.168.3.99:56994 192.168.3.212:6379 TIME_WAIT 0
C:\Users\sas>netstat --help
显示协议统计信息和当前 TCP/IP 网络连接。
NETSTAT [-a] [-b] [-e] [-f] [-n] [-o] [-p proto] [-r] [-s] [-x] [-t] [interval]
-a 显示所有连接和侦听端口。
-b 显示在创建每个连接或侦听端口时涉及的
可执行程序。在某些情况下,已知可执行程序承载
多个独立的组件,这些情况下,
显示创建连接或侦听端口时
涉及的组件序列。在此情况下,可执行程序的
名称位于底部 [] 中,它调用的组件位于顶部,
直至达到 TCP/IP。注意,此选项
可能很耗时,并且在你没有足够
权限时可能失败。
-e 显示以太网统计信息。此选项可以与 -s 选项
结合使用。
-f 显示外部地址的完全限定
域名(FQDN)。
-n 以数字形式显示地址和端口号。
-o 显示拥有的与每个连接关联的进程 ID。
-p proto 显示 proto 指定的协议的连接;proto
可以是下列任何一个: TCP、UDP、TCPv6 或 UDPv6。如果与 -s
选项一起用来显示每个协议的统计信息,proto 可以是下列任何一个:
IP、IPv6、ICMP、ICMPv6、TCP、TCPv6、UDP 或 UDPv6。
-q 显示所有连接、侦听端口和绑定的
非侦听 TCP 端口。绑定的非侦听端口
不一定与活动连接相关联。
-r 显示路由表。
-s 显示每个协议的统计信息。默认情况下,
显示 IP、IPv6、ICMP、ICMPv6、TCP、TCPv6、UDP 和 UDPv6 的统计信息;
-p 选项可用于指定默认的子网。
-t 显示当前连接卸载状态。
-x 显示 NetworkDirect 连接、侦听器和共享
终结点。
-y 显示所有连接的 TCP 连接模板。
无法与其他选项结合使用。
interval 重新显示选定的统计信息,各个显示间暂停的
间隔秒数。按 CTRL+C 停止重新显示
统计信息。如果省略,则 netstat 将打印当前的
配置信息一次。
【数据库 连接 后 不关闭 占用端口号,将端口号 耗尽】
Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。. Error 10048 connecting to 192.168.3.212:6379. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。.
return self.execute_command(command, *pieces, **kwargs) [docs]class Redis(StrictRedis): """ Provides backwards compatibility with older versions of redis-py that changed arguments to some commands to be more Pythonic, sane, or by accident. """ # Overridden callbacks RESPONSE_CALLBACKS = dict_merge( StrictRedis.RESPONSE_CALLBACKS, { 'TTL': lambda r: r >= 0 and r or None, 'PTTL': lambda r: r >= 0 and r or None, } ) [docs] def pipeline(self, transaction=True, shard_hint=None): """ Return a new pipeline object that can queue multiple commands for later execution. ``transaction`` indicates whether all commands should be executed atomically. Apart from making a group of operations atomic, pipelines are useful for reducing the back-and-forth overhead between the client and server. """ return Pipeline( self.connection_pool, self.response_callbacks, transaction, shard_hint) [docs] def setex(self, name, value, time): """ Set the value of key ``name`` to ``value`` that expires in ``time`` seconds. ``time`` can be represented by an integer or a Python timedelta object. """ if isinstance(time, datetime.timedelta): time = time.seconds + time.days * 24 * 3600 return self.execute_command('SETEX', name, time, value) [docs] def lrem(self, name, value, num=0): """ Remove the first ``num`` occurrences of elements equal to ``value`` from the list stored at ``name``. The ``num`` argument influences the operation in the following ways: num > 0: Remove elements equal to value moving from head to tail. num < 0: Remove elements equal to value moving from tail to head. num = 0: Remove all elements equal to value. """ return self.execute_command('LREM', name, num, value) [docs] def zadd(self, name, *args, **kwargs): """ NOTE: The order of arguments differs from that of the official ZADD command. For backwards compatability, this method accepts arguments in the form of name1, score1, name2, score2, while the official Redis documents expects score1, name1, score2, name2. If you're looking to use the standard syntax, consider using the StrictRedis class. See the API Reference section of the docs for more information. Set any number of element-name, score pairs to the key ``name``. Pairs can be specified in two ways: As *args, in the form of: name1, score1, name2, score2, ... or as **kwargs, in the form of: name1=score1, name2=score2, ... The following example would add four values to the 'my-key' key: redis.zadd('my-key', 'name1', 1.1, 'name2', 2.2, name3=3.3, name4=4.4) """ pieces = [] if args: if len(args) % 2 != 0: raise RedisError("ZADD requires an equal number of " "values and scores") pieces.extend(reversed(args)) for pair in iteritems(kwargs): pieces.append(pair[1]) pieces.append(pair[0]) return self.execute_command('ZADD', name, *pieces) class PubSub(object):
rds = return_redis(redis_key) r = rds.scard(redis_info_tab) del rds
def return_redis(redis_key):
REDIS_HOST, REDIS_PORT, PASSWORD, db = redis_key
rds = Redis(host=REDIS_HOST, port=REDIS_PORT, password=PASSWORD, db=db)
return rds
del rds 并没有关闭与redis的连接,任然占用者端口号,导致端口号耗尽
Welcome to redis-py’s documentation! — redis-py 2.10.5 documentation http://redis-py.readthedocs.io/en/latest/
class redis.ConnectionPool(connection_class=<class 'redis.connection.Connection'>, max_connections=None, **connection_kwargs)[source]
class redis.ConnectionPool(connection_class=<class 'redis.connection.Connection'>, max_connections=None, **connection_kwargs)[source]
Generic connection pool
disconnect()[source]
Disconnects all connections in the pool
classmethod from_url(url, db=None, decode_components=False, **kwargs)[source]
Return a connection pool configured from the given URL.
For example:
redis://[:password]@localhost:6379/0
rediss://[:password]@localhost:6379/0
unix://[:password]@/path/to/socket.sock?db=0
Three URL schemes are supported:
`redis:// <http://www.iana.org/assignments/uri-schemes/prov/redis>`_ creates a normal TCP socket connection
`rediss:// <http://www.iana.org/assignments/uri-schemes/prov/rediss>`_ creates a SSL wrapped TCP socket connection
unix:// creates a Unix Domain Socket connection
There are several ways to specify a database number. The parse function will return the first specified option:
A db querystring option, e.g. redis://localhost?db=0
If using the redis:// scheme, the path argument of the url, e.g. redis://localhost/0
The db argument to this function.
If none of these options are specified, db=0 is used.
The decode_components argument allows this function to work with percent-encoded URLs. If this argument is set to True all %xx escapes will be replaced by their single-character equivalents after the URL has been parsed. This only applies to the hostname, path, and password components.
Any additional querystring arguments and keyword arguments will be passed along to the ConnectionPool class’s initializer. The querystring arguments socket_connect_timeout and socket_timeout if supplied are parsed as float values. The arguments socket_keepalive and retry_on_timeout are parsed to boolean values that accept True/False, Yes/No values to indicate state. Invalid types cause a UserWarning to be raised. In the case of conflicting arguments, querystring arguments always win.
get_connection(command_name, *keys, **options)[source]
Get a connection from the pool
get_encoder()[source]
Return an encoder based on encoding settings
make_connection()[source]
Create a new connection
release(connection)[source]
Releases the connection back to the pool
关闭连接池所有连接
将该连接释放回连接池
Welcome to redis-py’s documentation! — redis-py 2.10.5 documentation http://redis-py.readthedocs.io/en/latest/
http://redis-py.readthedocs.io/en/latest/_modules/redis/connection.html#ConnectionPool
class ConnectionPool(object): "Generic connection pool" @classmethod def from_url(cls, url, db=None, decode_components=False, **kwargs): """ Return a connection pool configured from the given URL. For example:: redis://[:password]@localhost:6379/0 rediss://[:password]@localhost:6379/0 unix://[:password]@/path/to/socket.sock?db=0 Three URL schemes are supported: - ```redis://`` <http://www.iana.org/assignments/uri-schemes/prov/redis>`_ creates a normal TCP socket connection - ```rediss://`` <http://www.iana.org/assignments/uri-schemes/prov/rediss>`_ creates a SSL wrapped TCP socket connection - ``unix://`` creates a Unix Domain Socket connection There are several ways to specify a database number. The parse function will return the first specified option: 1. A ``db`` querystring option, e.g. redis://localhost?db=0 2. If using the redis:// scheme, the path argument of the url, e.g. redis://localhost/0 3. The ``db`` argument to this function. If none of these options are specified, db=0 is used. The ``decode_components`` argument allows this function to work with percent-encoded URLs. If this argument is set to ``True`` all ``%xx`` escapes will be replaced by their single-character equivalents after the URL has been parsed. This only applies to the ``hostname``, ``path``, and ``password`` components. Any additional querystring arguments and keyword arguments will be passed along to the ConnectionPool class's initializer. The querystring arguments ``socket_connect_timeout`` and ``socket_timeout`` if supplied are parsed as float values. The arguments ``socket_keepalive`` and ``retry_on_timeout`` are parsed to boolean values that accept True/False, Yes/No values to indicate state. Invalid types cause a ``UserWarning`` to be raised. In the case of conflicting arguments, querystring arguments always win. """ url_string = url url = urlparse(url) qs = '' # in python2.6, custom URL schemes don't recognize querystring values # they're left as part of the url.path. if '?' in url.path and not url.query: # chop the querystring including the ? off the end of the url # and reparse it. qs = url.path.split('?', 1)[1] url = urlparse(url_string[:-(len(qs) + 1)]) else: qs = url.query url_options = {} for name, value in iteritems(parse_qs(qs)): if value and len(value) > 0: parser = URL_QUERY_ARGUMENT_PARSERS.get(name) if parser: try: url_options[name] = parser(value[0]) except (TypeError, ValueError): warnings.warn(UserWarning( "Invalid value for `%s` in connection URL." % name )) else: url_options[name] = value[0] if decode_components: password = unquote(url.password) if url.password else None path = unquote(url.path) if url.path else None hostname = unquote(url.hostname) if url.hostname else None else: password = url.password path = url.path hostname = url.hostname # We only support redis:// and unix:// schemes. if url.scheme == 'unix': url_options.update({ 'password': password, 'path': path, 'connection_class': UnixDomainSocketConnection, }) else: url_options.update({ 'host': hostname, 'port': int(url.port or 6379), 'password': password, }) # If there's a path argument, use it as the db argument if a # querystring value wasn't specified if 'db' not in url_options and path: try: url_options['db'] = int(path.replace('/', '')) except (AttributeError, ValueError): pass if url.scheme == 'rediss': url_options['connection_class'] = SSLConnection # last shot at the db value url_options['db'] = int(url_options.get('db', db or 0)) # update the arguments from the URL values kwargs.update(url_options) # backwards compatability if 'charset' in kwargs: warnings.warn(DeprecationWarning( '"charset" is deprecated. Use "encoding" instead')) kwargs['encoding'] = kwargs.pop('charset') if 'errors' in kwargs: warnings.warn(DeprecationWarning( '"errors" is deprecated. Use "encoding_errors" instead')) kwargs['encoding_errors'] = kwargs.pop('errors') return cls(**kwargs) def __init__(self, connection_class=Connection, max_connections=None, **connection_kwargs): """ Create a connection pool. If max_connections is set, then this object raises redis.ConnectionError when the pool's limit is reached. By default, TCP connections are created unless connection_class is specified. Use redis.UnixDomainSocketConnection for unix sockets. Any additional keyword arguments are passed to the constructor of connection_class. """ max_connections = max_connections or 2 ** 31 if not isinstance(max_connections, (int, long)) or max_connections < 0: raise ValueError('"max_connections" must be a positive integer') self.connection_class = connection_class self.connection_kwargs = connection_kwargs self.max_connections = max_connections self.reset() def __repr__(self): return "%s<%s>" % ( type(self).__name__, self.connection_class.description_format % self.connection_kwargs, ) def reset(self): self.pid = os.getpid() self._created_connections = 0 self._available_connections = [] self._in_use_connections = set() self._check_lock = threading.Lock() def _checkpid(self): if self.pid != os.getpid(): with self._check_lock: if self.pid == os.getpid(): # another thread already did the work while we waited # on the lock. return self.disconnect() self.reset() def get_connection(self, command_name, *keys, **options): "Get a connection from the pool" self._checkpid() try: connection = self._available_connections.pop() except IndexError: connection = self.make_connection() self._in_use_connections.add(connection) return connection def get_encoder(self): "Return an encoder based on encoding settings" kwargs = self.connection_kwargs return Encoder( encoding=kwargs.get('encoding', 'utf-8'), encoding_errors=kwargs.get('encoding_errors', 'strict'), decode_responses=kwargs.get('decode_responses', False) ) def make_connection(self): "Create a new connection" if self._created_connections >= self.max_connections: raise ConnectionError("Too many connections") self._created_connections += 1 return self.connection_class(**self.connection_kwargs) def release(self, connection): "Releases the connection back to the pool" self._checkpid() if connection.pid != self.pid: return self._in_use_connections.remove(connection) self._available_connections.append(connection) def disconnect(self): "Disconnects all connections in the pool" all_conns = chain(self._available_connections, self._in_use_connections) for connection in all_conns: connection.disconnect()
重启电脑
C:\Users\sas>netstat -ano 活动连接 协议 本地地址 外部地址 状态 PID TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 324 TCP 0.0.0.0:443 0.0.0.0:0 LISTENING 5060 TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4 TCP 0.0.0.0:902 0.0.0.0:0 LISTENING 3368 TCP 0.0.0.0:912 0.0.0.0:0 LISTENING 3368 TCP 0.0.0.0:49664 0.0.0.0:0 LISTENING 628 TCP 0.0.0.0:49665 0.0.0.0:0 LISTENING 1488 TCP 0.0.0.0:49666 0.0.0.0:0 LISTENING 1348 TCP 0.0.0.0:49667 0.0.0.0:0 LISTENING 2332 TCP 0.0.0.0:49668 0.0.0.0:0 LISTENING 2384 TCP 0.0.0.0:49673 0.0.0.0:0 LISTENING 776 TCP 0.0.0.0:49676 0.0.0.0:0 LISTENING 788 TCP 127.0.0.1:5939 0.0.0.0:0 LISTENING 3568 TCP 127.0.0.1:8307 0.0.0.0:0 LISTENING 5060 TCP 192.168.3.99:139 0.0.0.0:0 LISTENING 4 TCP 192.168.3.99:5040 0.0.0.0:0 LISTENING 6584 TCP 192.168.3.99:49670 61.155.185.193:8099 ESTABLISHED 3408 TCP 192.168.3.99:49671 185.48.81.253:443 LAST_ACK 4380 TCP 192.168.3.99:49674 52.230.83.250:443 ESTABLISHED 3308 TCP 192.168.3.99:49675 202.89.233.100:443 ESTABLISHED 6072 TCP 192.168.3.99:49677 23.206.234.50:443 ESTABLISHED 4380 TCP 192.168.3.99:49679 23.206.234.50:443 ESTABLISHED 4380 TCP 192.168.3.99:49680 23.209.7.122:80 ESTABLISHED 4380 TCP 192.168.3.99:49681 23.209.7.122:80 ESTABLISHED 4380 TCP 192.168.3.99:49682 23.209.6.217:443 ESTABLISHED 7052 TCP 192.168.3.99:49683 131.253.14.231:443 ESTABLISHED 7052 TCP 192.168.3.99:49685 23.209.6.217:443 ESTABLISHED 4968 TCP 192.168.3.99:49687 131.253.14.231:443 ESTABLISHED 4968 TCP 192.168.3.99:49691 220.181.76.76:80 ESTABLISHED 8596 TCP 192.168.3.99:49692 220.181.76.76:80 ESTABLISHED 8596 TCP 192.168.3.99:49693 220.181.76.76:80 ESTABLISHED 8596 TCP 192.168.3.99:49694 220.181.76.76:80 ESTABLISHED 8596 TCP 192.168.3.99:49695 220.181.76.76:80 ESTABLISHED 8596 TCP 192.168.3.99:49696 220.181.76.76:80 ESTABLISHED 8596 TCP 192.168.3.99:49697 220.181.76.76:80 ESTABLISHED 8596 TCP 192.168.3.99:49698 220.181.76.76:80 ESTABLISHED 8596 TCP 192.168.3.99:49699 220.181.76.76:80 ESTABLISHED 8596 TCP 192.168.3.99:49700 220.181.76.76:80 ESTABLISHED 8596 TCP 192.168.3.99:49701 180.88.61.115:80 ESTABLISHED 8596 TCP 192.168.3.99:49702 220.181.76.82:80 ESTABLISHED 8596 TCP 192.168.3.99:49703 220.181.76.82:80 ESTABLISHED 8596 TCP 192.168.56.1:139 0.0.0.0:0 LISTENING 4 TCP 192.168.56.1:5040 0.0.0.0:0 LISTENING 6584 TCP 192.168.157.1:139 0.0.0.0:0 LISTENING 4 TCP 192.168.157.1:5040 0.0.0.0:0 LISTENING 6584 TCP 192.168.181.2:139 0.0.0.0:0 LISTENING 4 TCP 192.168.181.2:5040 0.0.0.0:0 LISTENING 6584 TCP [::]:135 [::]:0 LISTENING 324 TCP [::]:443 [::]:0 LISTENING 5060 TCP [::]:445 [::]:0 LISTENING 4 TCP [::]:49664 [::]:0 LISTENING 628 TCP [::]:49665 [::]:0 LISTENING 1488 TCP [::]:49666 [::]:0 LISTENING 1348 TCP [::]:49667 [::]:0 LISTENING 2332 TCP [::]:49668 [::]:0 LISTENING 2384 TCP [::]:49673 [::]:0 LISTENING 776 TCP [::]:49676 [::]:0 LISTENING 788 TCP [::1]:8307 [::]:0 LISTENING 5060 UDP 0.0.0.0:5050 *:* 6584 UDP 0.0.0.0:5353 *:* 2364 UDP 0.0.0.0:5355 *:* 2364 UDP 0.0.0.0:50689 *:* 3396 UDP 0.0.0.0:58053 *:* 3568 UDP 0.0.0.0:58969 *:* 8596 UDP 192.168.3.99:137 *:* 4 UDP 192.168.3.99:138 *:* 4 UDP 192.168.3.99:5353 *:* 3568 UDP 192.168.56.1:137 *:* 4 UDP 192.168.56.1:138 *:* 4 UDP 192.168.56.1:5353 *:* 3568 UDP 192.168.157.1:137 *:* 4 UDP 192.168.157.1:138 *:* 4 UDP 192.168.157.1:5353 *:* 3568 UDP 192.168.181.2:137 *:* 4 UDP 192.168.181.2:138 *:* 4 UDP 192.168.181.2:5353 *:* 3568 UDP [::]:5353 *:* 2364 UDP [::]:5355 *:* 2364 UDP [::]:58054 *:* 3568 UDP [::1]:5353 *:* 3568 UDP [fe80::25b7:bc95:542:961b%11]:546 *:* 1852 UDP [fe80::3133:5fba:bc35:9775%6]:546 *:* 1852 UDP [fe80::49fd:25ea:7611:b235%15]:546 *:* 1852 UDP [fe80::6df9:9aae:efd6:871b%7]:546 *:* 1852 C:\Users\sas>
del 并没有释放或关闭连接
有关 redis-py 连接池会导致服务器产生大量 CLOSE_WAIT 的再讨论以及一个解决方案
有关 redis-py 连接池会导致服务器产生大量 CLOSE_WAIT 的再讨论以及一个解决方案 - V2EX https://www.v2ex.com/t/416910