I have a server based on twisted reactor, and I need the following scenario: the server can receive two types of request
- ADD(x,y) and returns a sum
- HUMAN_PERMISSION which returns true only if the human user approved the ip of the client
I am using tkMessageBox to ask the human user, but the problem is that it blocks the whole reactor and the server becomes unresponsive to other requests
I understand that I have to use twisted's deferred here in some way, just dont know how this: doesnt seem to work, it still blocks the whole reactor
d = deferLater(reactor, 1,tkMessageBox.showinfo, "is he ok?", clientIp)
d.addCallback(confirmUser)
Deferreds can't do anything to make your code non-blocking. All they can do is manage a callback chain based on an existing non-blocking event. That's what lets you translate low-level events like "some bytes were received" or "a connection was lost" or "a user clicked on a button" into high level events like "the HTTP request was responded to" or "the user answered your question".
deferLater, for example, simply fires itsDeferredwhen some time has passed.You don't need a reactor to use a
Deferred, even. For example:You can call
callback()from anywhere; it's just usually called from a reactor event. In your case, you probably want to callcallbackfrom aTkevent instead.All that said, you need a way to get
Tkevents into the main reactor thread, which you do by using a reactor that knows aboutTk's mainloop. A commenter already mentioned that there is an existing API for this:twisted.internet.tksupport. Given thatTkis not the most popular GUI these days, you may find some issues, so please report them if you find any.