python cgi脚本中的线程错误

时间:2013-01-22 22:01:09

标签: python linux multithreading apache cgi

我正在运行的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服务器的配置问题?这是我第一次看到这个错误,但这也是我第一次使用我正在使用的数据集运行这个应用程序。该数据集生成数千个线程。感谢。

1 个答案:

答案 0 :(得分:0)

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

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

在基于Linux的系统上,pthread用于实现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.