2013-05-12 43 views
1

我有一個瓶子的web應用程序。在某些時候,我希望服務器提出一個對話窗口來詢問服務器管理員的某些事情。即使從Thread開始,此警報也被阻止 - 我不明白爲什麼。蟒蛇win32api阻止瓶路線

若要查看此ctypes MessageBox是否阻塞,我試圖在一個最小示例上的線程上運行它。我試過這個例子:

import threading 
from threading import Thread 
import ctypes 
import time 
MessageBox = ctypes.windll.user32.MessageBoxA 

def alert(): 
    userChoice = MessageBox(0, "And this is crazy", "Hey I just met you",4) 
    threading.Timer(3.0,alert).start() 

worker = Thread(target=alert) 
worker.setDaemon(False) 
worker.start() 

while (True): 
    print("main thread is printing") 
    time.sleep(2) 

在這裏,主線程持續打印2秒間隔。同時,每3秒鐘顯示從線程開始的警報方法。我們清楚地看到循環不等待對話框返回值。

儘管進行了此測試,但在嘗試類似瓶子應用程序的代碼時,直到在對話框中單擊「是」或「否」,服務器纔會響應其路由。相反,它會等待對話框返回一個值,這意味着對話框會在某個級別阻止執行。

任何人都知道如何在不干擾瓶子工作的情況下舉辦對話?我正在用盡想法。感謝您的時間和精力。


UPDATE:
這是沒有問題的。瓶子在沒有干擾的情況下運行。實際問題在這裏描述得更好:bottle gevent and threading: gevent is only usable from a single thread

回答

1

您可能在您的瓶子應用程序中使用Gevent。如果您使用monkey.patch_all(),您的線程將變爲串行,並將阻止瓶子執行。

你不應該修補的線程:

from gevent import monkey 
monkey.patch_all(thread=False) 
+0

這就提出了另一個問題,http://stackoverflow.com/questions/16517796/bottle-gevent-and-threading-gevent-is-only-usable-從-A-單線程 – user1555863 2013-05-13 08:37:53