![](/img/trans.png)
[英]Django non blocking email? Downsides to threading.thread or subprocess?
[英]Subprocess in a threading.Thread hang
我不明白為什么下面的代碼掛起。 當我從控制台運行它時,一切都很好,但是當我嘗試通過瀏覽器運行它時,頁面將掛起。 我正在使用Python 2.7.2。
class MyThread(threading.Thread):
def __init__(self):
self.stdout = None
self.stderr = None
threading.Thread.__init__(self, name="snapshot",)
def run(self):
p = subprocess.Popen(["pwd"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
p.communicate()
myThread = MyThread()
myThread.start()
myThread.join()
更新我正在將Apache作為CGI運行Python。 正在運行的系統是Gentoo 2.1。
我認為問題出在Apache。 它以某種方式阻止了新線程,但是我不確定。
嘗試使用shell=False
運行它。
我懷疑當您在Web框架中運行此代碼時,實際上並沒有外殼,而這會引起問題。
或者,您無法重用Web服務器進程中的stdout
和stdin
,而嘗試通過subprocess.PIPE
這樣做會導致掛起。
我認為您實際上並不需要運行pwd
而只是以它為例,但是如果您確實需要pwd
這是一種不必要的復雜方法。 只需將其替換為os.curdir
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.