问题
I have a bottle web application. At some point, i want the server to raise a dialog window to ask the server admin for something. This alert, even when started from a Thread
is blocking - And i don't really understand why.
To see if this ctypes MessageBox is blocking, i've tried to run it on a thread on a minimal example. I've tried this example:
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)
Here, the main thread keeps on printing on 2 seconds interval. Concurrently, each 3 seconds the alert method started from a thread is shown. We clearly see that the loop isn't waiting for the dialog to return a value.
Despite this test, when trying similar code from a bottle application, until 'Yes' or 'No' is clicked on the dialog, the server does not respond to it's routes. Instead, it waits for the dialog to return a value, which means the dialog is blocking execution at some level.
Anyone knows how to raise a dialog without interfering with bottle work? I'm running out of ideas. Appreciate your time and effort.
UPDATE:
This isn't the problem. Bottle does run with this example with out interference. The actual issue is better described here: bottle gevent and threading: gevent is only usable from a single thread
回答1:
You probably use Gevent with your bottle application . If you monkey.patch_all() , your threads become serial , and will block bottle execution .
You should not patch your threads :
from gevent import monkey
monkey.patch_all(thread=False)
来源:https://stackoverflow.com/questions/16505586/python-win32api-blocking-bottle-routes