Hi. I was going through some older posts looking for current material on twisted integration and found this which I am hoping to use as a bit of a base to build on. Almost all examples of twisted I can find invoke run() and seem to use wxreactor(deprecated from what I have read). This method does not, so I am hoping a small example of using it will shed some light. Can someone elaborate on this method to get a web page using twisted.web.client.getPage so that I can follow this through a bit more clearly.
Many thanks,
David
from twisted.web import client
from twisted.internet import threadedselectreactor
from twisted.internet.main import installReactor
class App(wx.App):
def OnInit(self):
self.connections = []
# Set up twisted reactor
reactor = threadedselectreactor.ThreadedSelectReactor()
reactor.interleave(wx.CallAfter)
installReactor(reactor)
# Expose reactor as part of App
self.reactor = reactor
# Top level frame
self.frame = Frame()
self.frame.Show()
return True
def ExitMainLoop(self):
self.reactor.stop()
self.WaitOnReactor()
def WaitOnReactor(self):
if self.reactor.running:
# Wait for twisted reactor shutdown
wx.CallAfter(self.WaitOnReactor)
else:
# Shutdown
wx.App.ExitMainLoop(self)
David Pratt wrote:
Can someone elaborate on this method to get a web page using twisted.web.client.getPage so that I can follow this through a bit more clearly.
Hi David,
I use this code that talk with wx and twisted for my projects:
import wx
from twisted.internet import threadedselectreactor
threadedselectreactor.install()
from twisted.internet import reactor
class frame(wx.Frame):
def __init__(self):
super(frame, self).__init__(None)
self.Bind(wx.EVT_CLOSE, self.OnClose)
def onClose(self, evt):
reactor.addSystemEventTrigger('after', 'shutdown', self._internalClose, True)
try: reactor.stop()
except: pass
def _internalClose(self, *args, **kargs):
""" Close twisted and the application
"""
self.Destroy()
class MyApp(wx.App):
def OnInit(self):
reactor.interleave(wx.CallAfter)
return True
if __name__ == '__main__':
app = MyApp(0)
frame = frame()
frame.Show()
app.MainLoop()
Here I didn't add the twisted.web.client.getPage functions, but I think
that is simple to do it.
Michele
wxreactor uses threadedselectreactor "under the hood" but it's set up
so that you control the app lifetime via the reactor, which isn't what
I wanted (I'm the OP of the code you're using), which is why I drive
it manually from the app like this.
Once you have both twisted and wx running interleaved, you can use
Twisted exactly as you would in a non-wx application - just follow any
of the other Twisted sample code.
···
On 2/5/06, David Pratt <fairwinds@eastlink.ca> wrote:
Hi. I was going through some older posts looking for current material on
twisted integration and found this which I am hoping to use as a bit of
a base to build on. Almost all examples of twisted I can find invoke
run() and seem to use wxreactor(deprecated from what I have read). This
method does not, so I am hoping a small example of using it will shed
some light. Can someone elaborate on this method to get a web page using
twisted.web.client.getPage so that I can follow this through a bit more
clearly.
Many thanks,
David
from twisted.web import client
from twisted.internet import threadedselectreactor
from twisted.internet.main import installReactor
class App(wx.App):
def OnInit(self):
self.connections =
# Set up twisted reactor
reactor = threadedselectreactor.ThreadedSelectReactor()
reactor.interleave(wx.CallAfter)
installReactor(reactor)
# Expose reactor as part of App
self.reactor = reactor
# Top level frame
self.frame = Frame()
self.frame.Show()
return True
def ExitMainLoop(self):
self.reactor.stop()
self.WaitOnReactor()
def WaitOnReactor(self):
if self.reactor.running:
# Wait for twisted reactor shutdown
wx.CallAfter(self.WaitOnReactor)
else:
# Shutdown
wx.App.ExitMainLoop(self)
Hi Michele. Many thanks for your reply. This is extremely helpful and I have it working with the simple hello world example printing statements every second. The next issue for me to work out is the best way to give the reactor something to do from the wx app and opposite (giving wx app something to do as a response from the reactor). Could you advise an approach that has been effective for this interaction. Unfortunately the twisted wxdemo does not show this off.
Regards
David
Michele Petrazzo wrote:
···
David Pratt wrote:
Can someone elaborate on this method to get a web page using twisted.web.client.getPage so that I can follow this through a bit more clearly.
Hi David,
I use this code that talk with wx and twisted for my projects:
Hi Chris. Thank you for your reply. The responses to this thread are helping quite a bit. When you mean driving the reactor, I see that it is an attribute of the app but not how you run it from the attribute. I was using simple wxdemo hello world method ...
def helloWorld():
print "hello, world"
reactor.callLater(1, helloWorld)
reactor.callLater(1, helloWorld)
def twoSecondsPassed():
print "two seconds passed"
reactor.callLater(2, twoSecondsPassed)
reactor.callLater(2, twoSecondsPassed)
It did not run since it seemed the reactor was not yet active. Could you elaborate on using the app's reactor attribute by providing a brief example. Many thanks.
Regards,
David
···
wxreactor uses threadedselectreactor "under the hood" but it's set up
so that you control the app lifetime via the reactor, which isn't what
I wanted (I'm the OP of the code you're using), which is why I drive
it manually from the app like this.
Once you have both twisted and wx running interleaved, you can use
Twisted exactly as you would in a non-wx application - just follow any
of the other Twisted sample code.
---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org
Hi Chris and Michele. I tried a few things today. I am passing the reactor object into the frame during OnInit for my app (since these are different modules for me). From my frame I have made the reactor an attribute so I can obtain it easily and/or pass it as needed to other classes. I created a small test class in a test module (to test some twisted logic). I imported my test class, created an instance of it from my frame, and passed reactor object to it and it seems to work fine.
I subclass most everything for my app so I always have access to the parent(the main frame). As long as I have this I have access to the reactor which seems good.
Can you comment on whether this approach is appropriate.
On exitting app, I am doing this ...
def OnFileExit(self, event):
print 'Closing'
self.parent.Close(True)
wx.GetApp().ExitMainLoop()
Chris, I am using your ExitMainLoop logic to wait for twisted loop to complete before closing. This seems a very nice way to close the app so nothing gets lost. I added exception handling for this method in the event the loop dies or did not start properly so it will exit cleanly (otherwise it could throw RuntimeError exception under these circumstances)
Regards,
David
David Pratt wrote:
···
Hi Michele. Many thanks for your reply. This is extremely helpful and I have it working with the simple hello world example printing statements every second. The next issue for me to work out is the best way to give the reactor something to do from the wx app and opposite (giving wx app something to do as a response from the reactor). Could you advise an approach that has been effective for this interaction. Unfortunately the twisted wxdemo does not show this off.
Regards
David
Michele Petrazzo wrote:
David Pratt wrote:
Can someone elaborate on this method to get a web page using twisted.web.client.getPage so that I can follow this through a bit more clearly.
Hi David,
I use this code that talk with wx and twisted for my projects:
---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org
Hi Chris and Michele. I tried a few things today. I am passing the
reactor object into the frame during OnInit for my app (since these are
different modules for me). From my frame I have made the reactor an
attribute so I can obtain it easily and/or pass it as needed to other
classes. I created a small test class in a test module (to test some
twisted logic). I imported my test class, created an instance of it from
my frame, and passed reactor object to it and it seems to work fine.
I subclass most everything for my app so I always have access to the
parent(the main frame). As long as I have this I have access to the
reactor which seems good.
Can you comment on whether this approach is appropriate.
On exitting app, I am doing this ...
def OnFileExit(self, event):
print 'Closing'
self.parent.Close(True)
wx.GetApp().ExitMainLoop()
Chris, I am using your ExitMainLoop logic to wait for twisted loop to
complete before closing. This seems a very nice way to close the app so
nothing gets lost. I added exception handling for this method in the
event the loop dies or did not start properly so it will exit cleanly
(otherwise it could throw RuntimeError exception under these circumstances)
I thought my ExitMainLoop code worked better than it did until I broke
it trying to write you a sample It only works when you explicitly
exit the app by calling ExitMainLoop, not when you let the implicit
exit of closing all top level windows happen. So be sure that you exit
by doing that (as you are in your sample) and all should be well.
···
On 2/6/06, David Pratt <fairwinds@eastlink.ca> wrote:
Regards,
David
David Pratt wrote:
> Hi Michele. Many thanks for your reply. This is extremely helpful and I
> have it working with the simple hello world example printing statements
> every second. The next issue for me to work out is the best way to give
> the reactor something to do from the wx app and opposite (giving wx app
> something to do as a response from the reactor). Could you advise an
> approach that has been effective for this interaction. Unfortunately the
> twisted wxdemo does not show this off.
>
> Regards
> David
>
> Michele Petrazzo wrote:
>
>> David Pratt wrote:
>>
>>> Can someone elaborate on this method to get a web page using
>>> twisted.web.client.getPage so that I can follow this through a bit
>>> more clearly.
>>
>>
>>
>> Hi David,
>> I use this code that talk with wx and twisted for my projects:
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
> For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org