[twisted-python] Communication between GUI thread and twisted thread.
Hi, my program has two parts running in two threads. One is a GUI layer, the other is a twisted layer for communication with a remote server. Sometimes, a button is clicked in GUI to notice the twisted thread to send out some data. (reactor.callFromThread should be used, right?) In twisted part, I use reactor.connectTCP('x.x.x.x', port, MyFactory()) to connect with remote server. MyFactory will create an instance of MyProtocol, and MyProtocol has a method 'sendSpecialMsg' for doing real jobs. However, in GUI part, how can I invoke 'sendSpecialMsg' function? Even I cannot get the MyProtocol instance! Because connectTCP and MyFactory hide the protocol object. I'm sure it is a common problem for beginners, Could you give me some advice? Thank you. --- ShenLei
甜瓜 wrote:
Hi, my program has two parts running in two threads. One is a GUI layer, the other is a twisted layer for communication with a remote server. Sometimes, a button is clicked in GUI to notice the twisted thread to send out some data. (reactor.callFromThread should be used, right?)
In twisted part, I use reactor.connectTCP('x.x.x.x', port, MyFactory()) to connect with remote server. MyFactory will create an instance of MyProtocol, and MyProtocol has a method 'sendSpecialMsg' for doing real jobs.
However, in GUI part, how can I invoke 'sendSpecialMsg' function? Even I cannot get the MyProtocol instance! Because connectTCP and MyFactory hide the protocol object. I'm sure it is a common problem for beginners, Could you give me some advice?
Something like ClientCreator (a convenient way to connect and get a protocol instance when it is connected) with blockingCallFromThread (in SVN Twisted, so the calling thread can get access to the result of the deferred from ClientCreator) would probably do the trick. If these aren't directly usable, you may find inspiration from looking at their implementations. -Andrew.
Thank you for your kind reply.
Something like ClientCreator (a convenient way to connect and get a protocol instance when it is connected) with blockingCallFromThread (in SVN Twisted, so the calling thread can get access to the result of the deferred from ClientCreator) would probably do the trick.
ClientCreator is not proper for my case, because I need the re-connect ability. Well, actually, my project is based on this article: http://solipsis.netofpeers.com/wiki2/index.php/WxPython_And_Twisted_Example a VERY GOOD implementation for cooperation between wx and twisted. The hard nut is how to make DaemonLoop know the protocol instance which is connecting to the remote server. ^_^ --- ShenLei
On 6/18/07, 甜瓜
Thank you for your kind reply.
Something like ClientCreator (a convenient way to connect and get a protocol instance when it is connected) with blockingCallFromThread (in SVN Twisted, so the calling thread can get access to the result of the deferred from ClientCreator) would probably do the trick.
ClientCreator is not proper for my case, because I need the re-connect ability.
Well, actually, my project is based on this article: http://solipsis.netofpeers.com/wiki2/index.php/WxPython_And_Twisted_Example a VERY GOOD implementation for cooperation between wx and twisted. The hard nut is how to make DaemonLoop know the protocol instance which is connecting to the remote server. ^_^
---
This is a terrible implementation for cooperation between wx and twisted. At the time it was written, it was acceptable, but you really should be using the wxreactor now.
ClientCreator is not proper for my case, because I need the re-connect ability.
I made a tricky for access to the protocol instance created, but I don't know if this is sane for the structure of twisted. Is something like this: ---------- class P(Protocol): def connectionMade(self): self.factory.instance = self return ... def connectionLost(self): self.factory.instance = None class F(Factory): protocol = P instance = None ... ---------- then you can access to protocol instance inside the factory, if it's not None. If somebody see an inconsistency or a better sugestion please let me know. Francisco -- "El hombre se descubre cuando se mide con un obstáculo" (Saint Exupéry)
On Mon, 2007-06-18 at 14:37 +0800, 甜瓜 wrote:
Hi, my program has two parts running in two threads. One is a GUI layer, the other is a twisted layer for communication with a remote server.
If you're using a GUI supported by Twisted (Qt3, GTK, wxPython, tkinter) you don't need two threads: http://twistedmatrix.com/projects/core/documentation/howto/choosing-reactor....
Itamar Shtull-Trauring wrote:
On Mon, 2007-06-18 at 14:37 +0800, 甜瓜 wrote:
Hi, my program has two parts running in two threads. One is a GUI layer, the other is a twisted layer for communication with a remote server.
If you're using a GUI supported by Twisted (Qt3, GTK, wxPython, tkinter) you don't need two threads: http://twistedmatrix.com/projects/core/documentation/howto/choosing-reactor....
Indeed, I have found Twisted's asynchronous processing to make GUI programming much more intuitive and powerful. (I use PyQt, personally.) To a computer, a user is as much a deferrable input source as is a network connection. Best regards, Ed
This is a terrible implementation for cooperation between wx and twisted. At the time it was written, it was acceptable, but you really should be using the wxreactor now. Really? Oh! Maybe my knowledge is out of date. My opinion comes from "The Twisted Documentation - Jan 9, 2007". It says: "Twisted currently supports two methods of integrating wxPython. Unfortunately, neither method will work on all wxPython platforms (such as GTK2 or Windows). It seems that the only portable way to integrate with wxPython is to run it in a separate thread." ^_^ Therefore I followed that terrible implementation. Does wxreactor get improvement? I will read 'choosing-reactor.html' throughly. Thank you.
class P(Protocol): def connectionMade(self): self.factory.instance = self return Actually, I use the same trick now for workaround. ^_^
--- ShenLei
甜瓜 wrote:
This is a terrible implementation for cooperation between wx and twisted. At the time it was written, it was acceptable, but you really should be using the wxreactor now. Really? Oh! Maybe my knowledge is out of date. My opinion comes from "The Twisted Documentation - Jan 9, 2007". It says: "Twisted currently supports two methods of integrating wxPython. Unfortunately, neither method will work on all wxPython platforms (such as GTK2 or Windows). It seems that the only portable way to integrate with wxPython is to run it in a separate thread." ^_^ Therefore I followed that terrible implementation. Does wxreactor get improvement? I will read 'choosing-reactor.html' throughly. Thank you.
That section of the docs has not changed (current svn), and I agree it's rather confusing to determine what the actual recommendation is. My impression was that the most recent implementation of the wxreactor is supposed to work on all platforms. Is that not so? If it is, this section of the docs should probably be revised to recommend it more clearly. Steve -- "The opinions expressed above do not reflect official NASA policies or positions, and have not been reviewed by any Government authority."
On Mon, 18 Jun 2007 11:21:58 -0400, Stephen Waterbury
甜瓜 wrote:
This is a terrible implementation for cooperation between wx and twisted. At the time it was written, it was acceptable, but you really should be using the wxreactor now. Really? Oh! Maybe my knowledge is out of date. My opinion comes from "The Twisted Documentation - Jan 9, 2007". It says: "Twisted currently supports two methods of integrating wxPython. Unfortunately, neither method will work on all wxPython platforms (such as GTK2 or Windows). It seems that the only portable way to integrate with wxPython is to run it in a separate thread." ^_^ Therefore I followed that terrible implementation. Does wxreactor get improvement? I will read 'choosing-reactor.html' throughly. Thank you.
That section of the docs has not changed (current svn), and I agree it's rather confusing to determine what the actual recommendation is. My impression was that the most recent implementation of the wxreactor is supposed to work on all platforms. Is that not so? If it is, this section of the docs should probably be revised to recommend it more clearly.
It's so that wxreactor has been improved somewhat and that section of the documentation should be updated. However, for anyone seeking a recommendation, I will happily suggest PyGtk. ;) Jean-Paul
On 6/18/07, 甜瓜
This is a terrible implementation for cooperation between wx and twisted. At the time it was written, it was acceptable, but you really should be using the wxreactor now. Really? Oh! Maybe my knowledge is out of date. My opinion comes from "The Twisted Documentation - Jan 9, 2007". It says: "Twisted currently supports two methods of integrating wxPython. Unfortunately, neither method will work on all wxPython platforms (such as GTK2 or Windows). It seems that the only portable way to integrate with wxPython is to run it in a separate thread." ^_^ Therefore I followed that terrible implementation. Does wxreactor get improvement? I will read 'choosing-reactor.html' throughly. Thank you.
The docs are out of date in this case. Or rather, they are correct, but incomplete - Twisted now has support for a threaded reactor, and wxreactor has been updated to use this support, so the integration is now (mostly) seamless.
On Mon, 18 Jun 2007 10:32:50 -0500, Chris Mellon
On 6/18/07, 甜瓜
wrote: This is a terrible implementation for cooperation between wx and twisted. At the time it was written, it was acceptable, but you really should be using the wxreactor now. Really? Oh! Maybe my knowledge is out of date. My opinion comes from "The Twisted Documentation - Jan 9, 2007". It says: "Twisted currently supports two methods of integrating wxPython. Unfortunately, neither method will work on all wxPython platforms (such as GTK2 or Windows). It seems that the only portable way to integrate with wxPython is to run it in a separate thread." ^_^ Therefore I followed that terrible implementation. Does wxreactor get improvement? I will read 'choosing-reactor.html' throughly. Thank you.
The docs are out of date in this case. Or rather, they are correct, but incomplete - Twisted now has support for a threaded reactor, and wxreactor has been updated to use this support, so the integration is now (mostly) seamless.
To nit pick, http://twistedmatrix.com/buildbot-supported/ shows what Twisted has support for. What Twisted has for wx is "code". If you're lucky, it might even behave in a useful manner for your particular application. ;) For anyone interested in something that might be called "support", please get involved in Twisted development and contribute effort to finishing wxreactor. Jean-Paul
participants (8)
-
Andrew Bennetts
-
Chris Mellon
-
Ed Suominen
-
Francisco Herrero
-
Itamar Shtull-Trauring
-
Jean-Paul Calderone
-
Stephen Waterbury
-
甜瓜