python中的线程限制 [英] thread limit in python

查看:83
本文介绍了python中的线程限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


i遇到了一个问题,我无法在python程序中启动超过1021个线程

,无论我的ulimit是什么或者内核设置是。

我的程序崩溃''thread.error:当它没有启动新线程时'

试图启动第1021个线程。 $>

除了调整我的ulimit设置外,我还验证了

我使用的glibc版本已定义为PTHREAD_THREADS_MAX

16374.我已经在下面发布了我的测试程序以及我运行它的ulimit

设置。任何帮助将不胜感激。

提前感谢,

dan smith


< program>

导入os

导入系统

导入时间

导入线程

num_threads = int (sys.argv [1])


def run():

#只是睡觉

time.sleep(10000)


for i in range(1,num_threads + 1):


print''起始线程%d''%i

t = threading.Thread(无,运行)


t.start()


os._exit(1)< br $>
< / program>

< ulimit -a>

核心文件大小(块,-c)0

数据段大小(千字节,-d)无限

文件大小(块,-f)无限

最大锁定内存(千字节,-l) 4

最大内存大小(千字节,-m)无限

打开文件(-n)1000000

管道大小(512字节,-p )8

堆栈大小(kbytes,-s)无限制

cpu时间(秒,-t)无限制

最大用户进程(-u)无限

虚拟内存(kbytes,-v)无限

< / ulimit -a>

hello all,

i have run into a problem where i cannot start more than 1021 threads
in a python program, no matter what my ulimit or kernel settings are.
my program crashes with ''thread.error: can''t start new thread'' when it
tries to start the 1021st thread.

in addition to tweaking my ulimit settings, i have also verified that
the version of glibc i''m using has PTHREAD_THREADS_MAX defined to be
16374. i have posted my test program below as well as the ulimit
settings i was running it with. any help would be greatly appreciated.
thanks in advance,
dan smith

<program>
import os
import sys
import time
import threading

num_threads = int(sys.argv[1])

def run ():
# just sleep
time.sleep(10000)

for i in range(1,num_threads+1):

print ''starting thread %d'' %i
t=threading.Thread (None, run)

t.start()

os._exit(1)
</program>

<ulimit -a>
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) 4
max memory size (kbytes, -m) unlimited
open files (-n) 1000000
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) unlimited
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) unlimited
</ulimit -a>

推荐答案

,在同一个盒子上一个类似的C程序(在下面发布)没有问题

开始5000+个帖子。


#include< stdlib.h>

#include< stdio.h>

#include< sys / time.h>

#include< pthread.h>

无效*

run(void * arg){


sleep(1000);

}


int main(int argc,char * argv []){


int j;

pthread_t tid ;

int num_threads = atoi(argv [1]);


for(j = 0; j< NUM_THREADS; j ++){


pthread_create(& tid,NULL,run,NULL);

printf(" created thread%d \ n",j );

fflush(stdout);


}


sleep(1000);


}

also, on the same box a similar C program (posted below) has no problem
starting 5000+ threads.

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <pthread.h>
void *
run (void *arg) {

sleep(1000);
}

int main(int argc, char *argv[]) {

int j;
pthread_t tid;
int num_threads = atoi(argv[1]);

for (j=0; j < num_threads; j++) {

pthread_create (&tid, NULL, run, NULL);
printf("created thread %d\n",j);
fflush(stdout);

}

sleep(1000);

}


忽略C示例。没有检查

pthread_create的返回代码。在第1021个帖子中创建

时,C程序会在同一个地方中断。

disregard the C example. wasn''t checking the return code of
pthread_create. the C program breaks in the same place, when creating
the 1021st thread.


da ********** @ gmail.com 写道:
忽略C示例。没有检查
pthread_create的返回码。当创建第1021个线程时,C程序在同一个地方中断。
disregard the C example. wasn''t checking the return code of
pthread_create. the C program breaks in the same place, when creating
the 1021st thread.




所以这是一个很好的证据,它是一个操作系统限制,而不是
Python限制。最可能的问题是堆栈大小太大了,所以你的虚拟地址空间不足。

-

--Bryan



So that''s pretty good evidence that it''s an OS limit, not a
Python limit. The most likely problem is that the stack size is
too large, so you''re running out of virtual address space.
--
--Bryan


这篇关于python中的线程限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆