# 创建连接线程
threads = []
for _ in range(concurrent_connections):
t = threading.Thread(target=connect_to_database)
threads.append(t)
t.start()
# 等待所有线程完成
for t in threads:
t.join()
2.参数:
postgres=# show max_connections;
max_connections
-----------------
100
(1 row)
3.执行
[postgres@eulersvr ~]$ python3 conn_test.py
Exception in thread Thread-94 (connect_to_database):
Traceback (most recent call last):
File "/usr/lib64/python3.11/threading.py", line 1038, in _bootstrap_inner
self.run()
File "/usr/lib64/python3.11/threading.py", line 975, in run
self._target(*self._args, **self._kwargs)
File "/home/postgres/conn_test.py", line 16, in connect_to_database
conn = psycopg2.connect(**db_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.11/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.OperationalError: connection to server at "192.168.0.38", port 5432 failed: FATAL: sorry, too many clients already