0

我正在运行的 CGI 脚本出现以下错误......

Traceback (most recent call last):, referer: http://mysite/cgi-bin/dev/testing.py
  File "/var/www/cgi-bin/dev/testing.py", line 908, in <module>, referer: http://mysite/cgi-bin/dev/testing.py
    webpage(), referer: http://mysite/cgi-bin/dev/testing.py
  File "/var/www/cgi-bin/dev/testing.py", line 899, in webpage, referer: http://mysite/cgi-bin/dev/testing.py
    getResults(form), referer: http://mysite/cgi-bin/dev/testing.py
  File "/var/www/cgi-bin/dev/testing.py", line 557, in getResults, referer: http://mysite/cgi-bin/dev/testing.py
    new_nums = processNums(nums), referer: http://mysite/cgi-bin/dev/testing.py
  File "/var/www/cgi-bin/dev/testing.py", line 328, in processNums, referer: http://mysite/cgi-bin/dev/testing.py
    t.start(), referer: http://mysite/cgi-bin/dev/testing.py
  File "/usr/lib64/python2.6/threading.py", line 471, in start, referer: http://mysite/cgi-bin/dev/testing.py
    _start_new_thread(self.__bootstrap, ()), referer: http://mysite/cgi-bin/dev/testing.py
  thread.error: can't start new thread, referer: http://mysite/cgi-bin/dev/testing.py

这可能是我机器上的 ulimit 问题,但我想和你们一起检查我的代码。这是我用于线程的代码....

import Queue
import multiprocessing
from threading import Thread


def processNums(nums):
    new_nums = []

    queue = Queue.Queue()
    for num in nums:
        queue.put(num)

    thread_num = multiprocessing.cpu_count()
    for x in range(0,thread_num):
        t = Thread(target=multNum,args=(queue,new_nums,))
        t.setDaemon(True)
        t.start()

    queue.join()
    return new_nums


def multNum(queue,new_nums):
    while True:
        try: num = queue.get()
        except: break

        # do something....
        new_num = num*123456

        new_nums.append(new_num)
        queue.task_done()


print processNums([54,12,87,3268,2424,148,5,9877])

输出 [6666624, 1481472, 10740672, 403454208, 299257344, 18271488, 617280, 1219374912]

这是我的代码的一个真正淡化的版本(这里有很多我无法复制所有代码)但我怀疑我的问题出在此处。我的问题是......我应该以某种方式关闭这些线程吗?python不会自动做到这一点吗?或者这是 apache 或我的 linux 服务器的配置问题?这是我第一次看到这个错误,但这也是我第一次使用我正在使用的数据集运行这个应用程序。该数据集生成数千个线程。谢谢。

4

1 回答 1

0

完成后应该加入线程。请参阅Thread.join()的 Python 文档。

如果一个线程没有加入,它将作为一个僵尸进程保留在进程表中。

在基于 Linux 的系统上,pthreads 用于实现 Python 线程接口。很可能调用pthread_create失败。以下是手册页中的可能原因。这可能是一个EAGAIN错误,但错误代码无法通过 Python 线程接口提供。

  EAGAIN Insufficient  resources  to create another thread, or a system-imposed limit on the number of threads was encountered.
          The latter case may occur in two ways: the RLIMIT_NPROC soft resource limit (set via setrlimit(2)), which  limits  the
          number  of  process  for  a  real  user  ID,  was reached; or the kernel's system-wide limit on the number of threads,
          /proc/sys/kernel/threads-max, was reached.

   EINVAL Invalid settings in attr.

   EPERM  No permission to set the scheduling policy and parameters specified in attr.
于 2013-01-22T23:05:38.470 回答