Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何增加Twisted的连接池大小?_Python_Connection Pooling_Twisted - Fatal编程技术网

Python 如何增加Twisted的连接池大小?

Python 如何增加Twisted的连接池大小?,python,connection-pooling,twisted,Python,Connection Pooling,Twisted,我使用Twisted 8.1.0作为套接字服务器引擎。反应堆-epoll。数据库服务器是MySQL 5.0.67。操作系统-Ubuntu Linux 8.10 32位 在/etc/mysql/my.cnf中: max_connections = 1000 在源代码中: adbapi.ConnectionPool("MySQLdb", ..., use_unicode=True, charset='utf8', cp_min=3, cp

我使用Twisted 8.1.0作为套接字服务器引擎。反应堆-epoll。数据库服务器是MySQL 5.0.67。操作系统-Ubuntu Linux 8.10 32位

/etc/mysql/my.cnf
中:

max_connections        = 1000
在源代码中:

adbapi.ConnectionPool("MySQLdb", ..., use_unicode=True, charset='utf8', 
                      cp_min=3, cp_max=700, cp_noisy=False)
但实际上,当应用程序在重载下运行时,我只能看到200个(或更少)打开的连接(
showprocesslist
)。这对于我的应用程序来说是不够的:(


正如我所看到的,这是线程池的限制。有什么想法吗?

正如您所怀疑的,这可能是一个线程问题。
cp\u max
为线程池中的线程数设置了一个上限,但是,您的进程很可能会在低于此限制的情况下耗尽内存,大约200个线程。因为每个线程都有自己的线程堆栈,进程使用的总内存达到系统限制,无法创建更多线程

在运行程序之前,您可以通过调整堆栈大小ulimit设置(我正在使用
bash
)来检查这一点,即

$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
max nice                        (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 32750
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
max rt priority                 (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 32750
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
您可以看到,在我的计算机上,默认堆栈大小为10240K,我发现使用此设置可以创建大约300个线程。将堆栈大小调整为1024K(使用
ulimit-s 1024
)可以创建大约3000个线程

您可以使用以下脚本了解系统上的线程创建限制:

from thread import start_new_thread
from time import sleep

def sleeper():
    try:
        while 1:
            sleep(10000)
    except:
        if running: raise

def test():
    global running
    n = 0
    running = True
    try:
        while 1:
            start_new_thread(sleeper, ())
            n += 1
    except Exception, e:
        running = False
        print 'Exception raised:', e
    print 'Biggest number of threads:', n

if __name__ == '__main__':
    test()

这是否解决了您的问题将取决于
连接池
线程的内存需求。

也许您应该接受答案?此外,python>=2.5中的线程模块有一个stack_size()函数,您可以直接设置stack_size(最小32K).再次感谢!我只有11点声望,但需要15点才能投票:(