0

我在 Python 2.6.6 或 2.7.5 中使用简单的 BaseHTTPServer 文档声明它是单线程的,我在文档或 BaseHTTPServer 或 SocketServer 的源代码中找不到任何线索,事实并非如此。

但是下面的代码启动了一个有 2 个线程的应用程序(根据 ps 或 Taskmanager)。在 Windows 中就像在 Linux 中一样。

我没有得到什么?是否有可能拥有一个真正的单线程 baseHttpServer?

(我正在研究嵌入式系统并试图减少内存线程)

import BaseHTTPServer
httpd = BaseHTTPServer.HTTPServer(('', 8888), BaseHTTPServer.BaseHTTPRequestHandler)
httpd.serve_forever()

- - - - - - - - - - - 编辑

好吧,它变得更奇怪了:

  • 在 Windows 中,上面的代码显示了两个线程(任务管理器线程)
  • 在 linux(uc 或 mint)中,上面的代码显示了 1 个线程。

使用以下代码(更接近我的应用程序):

import BaseHTTPServer
import threading
import time

class test(threading.Thread):
    def stop(self):
         self.httpd.shutdown()
    def run(self):
        self.httpd = BaseHTTPServer.HTTPServer(('', 8888), BaseHTTPServer.BaseHTTPRequestHandler)
        self.httpd.serve_forever()
        print "killme"

t = test()
t.start()
try:
    while(True):
        time.sleep(1)
except:
    t.stop()

由此可见

  • 在 linux mint 2 线程中
  • 在 Windows 3 线程中
  • 在 uclinux 3 线程中

忙箱:

ps |grep test
PID USER       VSZ STAT COMMAND
12234 user      8476 S    python test.py
12244 user      8476 S    python test.py
12245 user      8476 S    python test.py
12252 user      2752 R    grep test

薄荷:

ps -L -F -A | grep test
UID        PID  PPID   LWP  C NLWP    SZ   RSS PSR STIME TTY          TIME CMD
deif      2446  2018  2446  0    2  5290  5432   0 15:30 pts/0    00:00:00 python test.py
deif      2446  2018  2447  0    2  5290  5432   0 15:30 pts/0    00:00:00 python test.py
4

0 回答 0