python cgi脚本中的线程错误 - python

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

参考方案

完成使用线程后,应将其连接。有关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.

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

python:ConfigParser对象,然后再阅读一次 - python

场景:我有一个配置文件,其中包含要执行的自动化测试的列表。这些测试是长期循环执行的。   配置文件的设计方式使ConfigParser可以读取它。由于有两个三个参数,因此我需要通过每个测试。现在,此配置文件由script(s1)调用,并且按照配置文件中的列表执行测试。Script(s1)第一次读取配置,并且在每次测试完成后都会执行。阅读两次的要求:由于可能会…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

python-docx应该在空单元格已满时返回空单元格 - python

我试图遍历文档中的所有表并从中提取文本。作为中间步骤,我只是尝试将文本打印到控制台。我在类似的帖子中已经看过scanny提供的其他代码,但是由于某种原因,它并没有提供我正在解析的文档的预期输出可以在https://www.ontario.ca/laws/regulation/140300中找到该文档from docx import Document from…