This script allows using twisted with wxPython at the same time without them stepping on each others toes. I'm so sorry for the messyness of it. I haven't found the time to tidy it up. The thing is it works, we use it in our kiosk administration program. <ad>http://www.sherborneinternational.com</ad>.
wxPython has its own main loop, twisted has its own main loop. wxreactor allows them to work together unless you want to use modal dialogs (and in my case didn't work on two linux machines with wx2.4). wxsupport is about the same, but didn't work on windows (with wx2.5) for me.
This solution is taken from itamar's suggestion in the twisted mailing list. Let each run in its own thread.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 | """This module enables wxPython and twisted to peacefully co-exist.
It does this by running wxPython in the main thread and starting a secondary thread for the twisted library.
It also provides a set of tools to enable communication between the two librarys
WARNING: DON'T use setTimeout on deferreds made/used in wx thread!
"""
import wx
from threading import Thread
from twisted.internet import reactor
from twisted.python import threadable
from twisted.internet.defer import maybeDeferred, Deferred
from time import sleep
# Make our own wxEvent for responding to messages
wxEVT_RESPONSE = wx.NewEventType()
class ResponseEvent(wx.PyCommandEvent):
"""This is our event used to pass net info
(success, failure, progress, and status)
to the gui"""
def __init__(self, func, onSuccess, onFailure, *params, **kwparams):
wx.PyCommandEvent.__init__(self, wxEVT_RESPONSE, 1)
self.func = func
self.onSuccess = onSuccess
self.onFailure = onFailure
self.params = params
self.kwparams = kwparams
class TwistedThread(Thread):
"""Starts twisted in a secondary thread.
It uses "commands" which are calls to the net thread from the gui thread
and "responses" which are calls to the gui thread from the net thread
but as both "commands" and "responses" have "onSuccess" and "onFailure"
callbacks which must again cross the thread boundary, these are reversed
so: A call from gui to net is a command, and its onSuccess call is a response
but a call from net to gui is a "response" and its onSuccess call is a "command"
so try not to get confused...
"""
def __init__(self, app, twistedLogFileName=None):
"""'app' is a wx.App instance"""
Thread.__init__(self)
self.app = None
self.twistedLogFileName = twistedLogFileName
self.setApp(app)
ThreadCommand.twistedThread = self
if app:
self.running = True
self.start()
def run(self):
threadable.init(1)
if self.twistedLogFileName:
from twisted.python import log
self.log = log
self.log.startLogging(open(self.twistedLogFileName, 'w'), 0)
self.log.msg('Started')
reactor.run(installSignalHandlers=0)
# Methods called from gui thread
def stop(self):
"""Call to cleanup the reactor"""
ThreadCommand((self._doStop, (), {}), self._onStopped, self._onStopFailed)
def _onStopped(self, res):
"""Called once the reactor has stopped"""
self.running = False
def _onStopFailed(self, reason):
self.running = False
raise Exception('Could not stop reactor: %s' % reason)
def setApp(self, app):
"""Call this first of all and every time you change
your application object (like in some testing programs)
It makes the app subscribe to our special events so that it can
call your callback functions.
"""
if app is not self.app:
if self.app:
self.app.Disconnect(1, 1, wxEVT_RESPONSE, self._runResponse)
self.app = app
self.app.Connect(1, 1, wxEVT_RESPONSE, self._runResponse)
def runCommand(self, tc):
"""Called from the gui thread, pass a ThreadCommand instance to the
network"""
reactor.callFromThread(self._doRunCommand, tc)
def _runResponse(self, evt):
"""Passes on a response from the net thread.
Called from wx main loop on reception of an wxEVT_RESPONSE"""
d = maybeDeferred(evt.func, *evt.params, **evt.kwparams)
if evt.onSuccess:
def onDone(r):
simpleCommand((evt.onSuccess, (r,), {}))
d.addCallback(onDone)
if evt.onFailure:
def onFail(r):
simpleCommand((evt.onFailure, (r,), {}))
d.addErrback(onFail)
# Methods called from net thread
def _doStop(self, tc):
reactor.stop()
def _doRunCommand(self, tc):
"""Called in the net thread to execute a gui command"""
# Run the command and get a deferred
if tc.passTC: d = maybeDeferred(tc.command[0], tc, *tc.command[1], **tc.command[2])
else: d = maybeDeferred(tc.command[0], *tc.command[1], **tc.command[2])
if tc.onSuccess: d.addCallback(self._success, tc)
if tc.onFailure: d.addErrback(self._failure, tc)
def _success(self, result, tc):
"""Called from the net thread. Appends a ThreadCommand
success callback to the gui thread's queue"""
if tc.param:
evt = ResponseEvent(tc.onSuccess, None, None, tc.param, result)
else:
evt = ResponseEvent(tc.onSuccess, None, None, result)
self.app.AddPendingEvent(evt)
def _failure(self, reason, tc):
"""Called from the net thread. Appends a ThreadCommand
failure callback to the gui thread's queue"""
if tc.param:
evt = ResponseEvent(tc.onFailure, None, None, tc.param, reason)
else:
evt = ResponseEvent(tc.onFailure, None, None, reason)
self.app.AddPendingEvent(evt)
# Constants for ThreadCommand states
QUEUED, RUNNING, SUCCEEDED, FAILED, CANCELLED = 'queued', 'running', 'succeeded', 'failed', 'cancelled'
class ThreadCommand(object):
"""This object represents a command from the gui
to the network library"""
twistedThread = None # Filled when a TwistedThread instance is created
def __init__(self, command, onSuccess, onFailure, param=None, onStatusReport=None, onProgressReport=None, onCustomReport=None, passTC=True):
"""'command' is a tuple containing a function reference, a sequence of parameters and a dictionary of parameters.
The func will be called but the first arg will be 'self' (this ThreadCommand instance)
'onSuccess' will be called passing 'param' and the result on success
'onFailure' will be called passing 'param' and an error message object on failure
'param' is passed to all callback funcs, unless it is 'None'
'onStatusReport' will be called passing a string for showing to the user along with param
'onProgressReport' will be called passing a float between 0 and 100 along with param
'onCustomReport' will be called with whatever params and kwparams the caller and server decide on,
but the first param will be 'param'
"""
self.command = command
self.onStatusReport = onStatusReport
self.onProgressReport = onProgressReport
self.onSuccess = onSuccess
self.onFailure = onFailure
self.onCustomReport = onCustomReport
self.param = param
self.state = QUEUED
self.passTC = passTC
# Queue ourselves or...
if self.twistedThread: self.twistedThread.runCommand(self)
else:
# Just run in current thread
import pdb
pdb.set_trace()
try:
if passTC: res = command[0](self, *command[1], **command[2])
else: res = command[0](*command[1], **command[2])
except Exception, e:
if onFailure:
if param: onFailure(e, param)
else: onFailure(e)
else:
if param: onFailure(e, param)
else: onFailure(e)
if onSuccess:
if isinstance(res, Deferred):
if param: res.addCallback(onSuccess, param)
else: res.addCallback(onSuccess)
else:
if param: onSuccess(res, param)
else: onSuccess(res)
# Methods called from the net thread
def progressReport(self, done, outOf=100):
"""Passes on a progress report to the gui thread"""
if self.param:
if outOf == 100: evt = ResponseEvent(self.onProgressReport, None, None, float(done), self.param)
else: evt = ResponseEvent(self.onProgressReport, None, None, (done/float(outOf))*100, self.param)
else:
if outOf == 100: evt = ResponseEvent(self.onProgressReport, None, None, float(done))
else: evt = ResponseEvent(self.onProgressReport, None, None, (done/float(outOf))*100)
self.twistedThread.app.AddPendingEvent(evt)
def statusReport(self, msg):
"""Passes on a status message to the gui"""
if self.param:
evt = ResponseEvent(self.onStatusReport, None, None, msg, self.param)
else:
evt = ResponseEvent(self.onStatusReport, None, None, msg)
self.twistedThread.app.AddPendingEvent(evt)
def customReport(self, *params, **kwparams):
"""Passes on a custom event to the gui thread"""
if self.param:
evt = ResponseEvent(self.onCustomReport, None, None, self.param, *params, **kwparams)
else:
evt = ResponseEvent(self.onCustomReport, None, None, *params, **kwparams)
self.twistedThread.app.AddPendingEvent(evt)
def simpleCommand(command, onSuccess=None, onFailure=None, param=None, onStatusReport=None, onProgressReport=None, onCustomReport=None):
"""A convinience function to allow you to call net commands without having to pass a tc"""
ThreadCommand(command, onSuccess, onFailure, param, onStatusReport, onProgressReport, onCustomReport, False)
def netCall(func, *params, **kwparams):
"""A nice wrapper function, returns a deferred.
Lets you safely call a function in the net thread
from the gui thread"""
d = Deferred()
simpleCommand((func, params, kwparams), d.callback, d.errback)
return d
def makeNetSafe(func):
"""Makes NET function safe for calling from the GUI thread.
Takes a func and returns a thread safe version of it
for calling a net func from the gui func"""
def result(*params, **kwparams):
return netCall(func, *params, **kwparams)
return result
def guiCall(func, *params, **kwparams):
"""Calls a gui func safely from the net thread"""
d = Deferred()
evt = ResponseEvent(func, d.callback, d.errback, *params, **kwparams)
ThreadCommand.twistedThread.app.AddPendingEvent(evt)
return d
def makeGuiSafe(func):
"""Makes a GUI function safe for calling from the NET thread.
Returns a wrapper func that allows you to
safely call the gui func 'func' from the net thread"""
def result(*params, **kwparams):
return guiCall(func, *params, **kwparams)
return result
---------------- 8< -----------------------------
Chat example files
---------------- 8< -----------------------------
chatDemo.wxg - A wxGlade file, used to generate chatExampleGui.py
---------------- 8< -----------------------------
<?xml version="1.0"?>
<!-- generated by wxGlade 0.3.2 on Mon Jul 05 14:12:09 2004 -->
<application path="C:\work\play\chatExample\chatExampleGui.py" name="" class="" option="0" language="python" top_window="" encoding="ANSI_X3.4-1968" use_gettext="0" overwrite="0" use_new_namespace="1">
<object class="ChatFrameGui" name="frmMain" base="EditFrame">
<style>wxDEFAULT_FRAME_STYLE</style>
<title>Chat</title>
<statusbar>1</statusbar>
<object class="wxStatusBar" name="frmMain_statusbar" base="EditStatusBar">
<fields>
<field width="-1">frmMain_statusbar</field>
</fields>
</object>
<object class="wxBoxSizer" name="sizer_1" base="EditBoxSizer">
<orient>wxVERTICAL</orient>
<object class="sizeritem">
<flag>wxEXPAND</flag>
<border>0</border>
<option>1</option>
<object class="wxPanel" name="panel_1" base="EditPanel">
<style>wxRAISED_BORDER|wxTAB_TRAVERSAL</style>
<object class="wxBoxSizer" name="sizer_3" base="EditBoxSizer">
<orient>wxVERTICAL</orient>
<object class="sizeritem">
<flag>wxEXPAND</flag>
<border>0</border>
<option>0</option>
<object class="wxPanel" name="panel_4" base="EditPanel">
<style>wxTAB_TRAVERSAL</style>
<object class="wxBoxSizer" name="sizer_4" base="EditBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<flag>wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL</flag>
<border>3</border>
<option>0</option>
<object class="wxStaticText" name="lblIpAddress" base="EditStaticText">
<attribute>1</attribute>
<label>IP &Address</label>
<size>51, 13</size>
</object>
</object>
<object class="sizeritem">
<flag>wxALL|wxEXPAND</flag>
<border>3</border>
<option>1</option>
<object class="wxTextCtrl" name="edtIPAddress" base="EditTextCtrl">
<tooltip>The ip address or host name of a remote machine running chat</tooltip>
<value>127.0.0.1</value>
</object>
</object>
<object class="sizeritem">
<flag>wxALL|wxEXPAND</flag>
<border>3</border>
<option>0</option>
<object class="wxSpinCtrl" name="spnConnectPort" base="EditSpinCtrl">
<style>wxSP_ARROW_KEYS|wxSP_WRAP</style>
<tooltip>The port on which the remote chat program is listening</tooltip>
<range>1, 65535</range>
<value>8080</value>
</object>
</object>
<object class="sizeritem">
<flag>wxALL</flag>
<border>3</border>
<option>0</option>
<object class="wxToggleButton" name="btnConnect" base="EditToggleButton">
<label>&Connect</label>
</object>
</object>
</object>
</object>
</object>
<object class="sizeritem">
<flag>wxEXPAND</flag>
<border>0</border>
<option>0</option>
<object class="wxPanel" name="panel_3" base="EditPanel">
<style>wxTAB_TRAVERSAL</style>
<object class="wxBoxSizer" name="sizer_6" base="EditBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<flag>wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL</flag>
<border>3</border>
<option>0</option>
<object class="wxStaticText" name="lblListenPort" base="EditStaticText">
<attribute>1</attribute>
<label>Listen &on port</label>
<size>64, 13</size>
</object>
</object>
<object class="sizeritem">
<flag>wxALL|wxEXPAND</flag>
<border>3</border>
<option>1</option>
<object class="wxSpinCtrl" name="spnListenPort" base="EditSpinCtrl">
<style>wxSP_ARROW_KEYS|wxSP_WRAP</style>
<tooltip>The port on which to listen for incoming connections</tooltip>
<range>1, 65535</range>
<value>8080</value>
</object>
</object>
<object class="sizeritem">
<flag>wxALL</flag>
<border>3</border>
<option>0</option>
<object class="wxToggleButton" name="btnListen" base="EditToggleButton">
<tooltip>Listen for incoming connections</tooltip>
<label>Lis&ten</label>
</object>
</object>
</object>
</object>
</object>
<object class="sizeritem">
<border>0</border>
<option>0</option>
<object class="wxStaticText" name="lblReceived" base="EditStaticText">
<attribute>1</attribute>
<label>Received</label>
</object>
</object>
<object class="sizeritem">
<flag>wxEXPAND</flag>
<border>0</border>
<option>1</option>
<object class="wxTextCtrl" name="edtReceived" base="EditTextCtrl">
<foreground>#000000</foreground>
<style>wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH2|wxTE_AUTO_URL</style>
<background>#c0c0c0</background>
</object>
</object>
<object class="sizeritem">
<border>0</border>
<option>0</option>
<object class="wxStaticText" name="lblSent" base="EditStaticText">
<attribute>1</attribute>
<label>Sent:</label>
</object>
</object>
<object class="sizeritem">
<flag>wxEXPAND</flag>
<border>0</border>
<option>1</option>
<object class="wxTextCtrl" name="edtSent" base="EditTextCtrl">
<style>wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH2|wxTE_AUTO_URL</style>
<background>#c0c0c0</background>
</object>
</object>
</object>
</object>
</object>
<object class="sizeritem">
<flag>wxEXPAND</flag>
<border>0</border>
<option>0</option>
<object class="wxPanel" name="panel_2" base="EditPanel">
<style>wxRAISED_BORDER|wxTAB_TRAVERSAL</style>
<object class="wxBoxSizer" name="sizer_2" base="EditBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<flag>wxALL|wxEXPAND</flag>
<border>5</border>
<option>1</option>
<object class="wxTextCtrl" name="edtToSend" base="EditTextCtrl">
</object>
</object>
<object class="sizeritem">
<flag>wxALL</flag>
<border>4</border>
<option>0</option>
<object class="wxButton" name="btnSend" base="EditButton">
<default>1</default>
<label>&Send</label>
</object>
</object>
<object class="sizeritem">
<flag>wxALL</flag>
<border>4</border>
<option>0</option>
<object class="wxButton" name="btnClose" base="EditButton">
<label>&Close</label>
</object>
</object>
</object>
</object>
</object>
</object>
</object>
</application>
---------------- 8< -----------------------------
chatExampleGui.py - The wx bits
---------------- 8< -----------------------------
#!/usr/bin/env python
# generated by wxGlade 0.3.2 on Sun Jul 4 09:38:17 2004
import wx
class ChatFrameGui(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: ChatFrameGui.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.panel_2 = wx.Panel(self, -1, style=wx.RAISED_BORDER|wx.TAB_TRAVERSAL)
self.panel_1 = wx.Panel(self, -1, style=wx.RAISED_BORDER|wx.TAB_TRAVERSAL)
self.panel_3 = wx.Panel(self.panel_1, -1)
self.panel_4 = wx.Panel(self.panel_1, -1)
self.frmMain_statusbar = self.CreateStatusBar(1)
self.lblIpAddress = wx.StaticText(self.panel_4, -1, "IP &Address")
self.edtIPAddress = wx.TextCtrl(self.panel_4, -1, "127.0.0.1")
self.spnConnectPort = wx.SpinCtrl(self.panel_4, -1, "8080", min=1, max=65535, style=wx.SP_ARROW_KEYS|wx.SP_WRAP)
self.btnConnect = wx.ToggleButton(self.panel_4, -1, "&Connect")
self.lblListenPort = wx.StaticText(self.panel_3, -1, "Listen &on port")
self.spnListenPort = wx.SpinCtrl(self.panel_3, -1, "8080", min=1, max=65535, style=wx.SP_ARROW_KEYS|wx.SP_WRAP)
self.btnListen = wx.ToggleButton(self.panel_3, -1, "Lis&ten")
self.lblReceived = wx.StaticText(self.panel_1, -1, "Received")
self.edtReceived = wx.TextCtrl(self.panel_1, -1, "", style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2|wx.TE_AUTO_URL)
self.lblSent = wx.StaticText(self.panel_1, -1, "Sent:")
self.edtSent = wx.TextCtrl(self.panel_1, -1, "", style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2|wx.TE_AUTO_URL)
self.edtToSend = wx.TextCtrl(self.panel_2, -1, "")
self.btnSend = wx.Button(self.panel_2, -1, "&Send")
self.btnClose = wx.Button(self.panel_2, -1, "&Close")
self.__set_properties()
self.__do_layout()
# end wxGlade
def __set_properties(self):
# begin wxGlade: ChatFrameGui.__set_properties
self.SetTitle("Chat")
self.frmMain_statusbar.SetStatusWidths([-1])
# statusbar fields
frmMain_statusbar_fields = ["frmMain_statusbar"]
for i in range(len(frmMain_statusbar_fields)):
self.frmMain_statusbar.SetStatusText(frmMain_statusbar_fields[i], i)
self.lblIpAddress.SetSize((51, 13))
self.edtIPAddress.SetToolTipString("The ip address or host name of a remote machine running chat")
self.spnConnectPort.SetToolTipString("The port on which the remote chat program is listening")
self.lblListenPort.SetSize((64, 13))
self.spnListenPort.SetToolTipString("The port on which to listen for incoming connections")
self.btnListen.SetToolTipString("Listen for incoming connections")
self.edtReceived.SetBackgroundColour(wx.Colour(192, 192, 192))
self.edtReceived.SetForegroundColour(wx.Colour(0, 0, 0))
self.edtSent.SetBackgroundColour(wx.Colour(192, 192, 192))
self.btnSend.SetDefault()
# end wxGlade
def __do_layout(self):
# begin wxGlade: ChatFrameGui.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
sizer_3 = wx.BoxSizer(wx.VERTICAL)
sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
sizer_4.Add(self.lblIpAddress, 0, wx.ALL|wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 3)
sizer_4.Add(self.edtIPAddress, 1, wx.ALL|wx.EXPAND, 3)
sizer_4.Add(self.spnConnectPort, 0, wx.ALL|wx.EXPAND, 3)
sizer_4.Add(self.btnConnect, 0, wx.ALL, 3)
self.panel_4.SetAutoLayout(1)
self.panel_4.SetSizer(sizer_4)
sizer_4.Fit(self.panel_4)
sizer_4.SetSizeHints(self.panel_4)
sizer_3.Add(self.panel_4, 0, wx.EXPAND, 0)
sizer_6.Add(self.lblListenPort, 0, wx.ALL|wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 3)
sizer_6.Add(self.spnListenPort, 1, wx.ALL|wx.EXPAND, 3)
sizer_6.Add(self.btnListen, 0, wx.ALL, 3)
self.panel_3.SetAutoLayout(1)
self.panel_3.SetSizer(sizer_6)
sizer_6.Fit(self.panel_3)
sizer_6.SetSizeHints(self.panel_3)
sizer_3.Add(self.panel_3, 0, wx.EXPAND, 0)
sizer_3.Add(self.lblReceived, 0, 0, 0)
sizer_3.Add(self.edtReceived, 1, wx.EXPAND, 0)
sizer_3.Add(self.lblSent, 0, 0, 0)
sizer_3.Add(self.edtSent, 1, wx.EXPAND, 0)
self.panel_1.SetAutoLayout(1)
self.panel_1.SetSizer(sizer_3)
sizer_3.Fit(self.panel_1)
sizer_3.SetSizeHints(self.panel_1)
sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
sizer_2.Add(self.edtToSend, 1, wx.ALL|wx.EXPAND, 5)
sizer_2.Add(self.btnSend, 0, wx.ALL, 4)
sizer_2.Add(self.btnClose, 0, wx.ALL, 4)
self.panel_2.SetAutoLayout(1)
self.panel_2.SetSizer(sizer_2)
sizer_2.Fit(self.panel_2)
sizer_2.SetSizeHints(self.panel_2)
sizer_1.Add(self.panel_2, 0, wx.EXPAND, 0)
self.SetAutoLayout(1)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
sizer_1.SetSizeHints(self)
self.Layout()
# end wxGlade
# end of class ChatFrameGui
---------------- 8< -----------------------------
chatExample.py - Notice the various different uses of the library.
Refer to comment above for easiest use (makeNetSafe, netCall, makeGuiSafe, guiCall). guiCall is not tested, haven't needed to use it yet.
---------------- 8< -----------------------------
import wx
from chatExampleGui import ChatFrameGui
from guinet import TwistedThread, ThreadCommand, netCall, makeNetSafe, guiCall, makeGuiSafe
from wxPython.lib.evtmgr import eventManager
from twisted.protocols.basic import LineReceiver
from twisted.internet.protocol import Factory, ClientFactory
from twisted.internet import reactor
from twisted.internet.defer import Deferred
from twisted.python.failure import Failure
###############################################################################
## Classes that run in the net thread #########################################
###############################################################################
class ChatProtocol(LineReceiver):
def __init__(self):
self.setLineMode()
def dataReceived(self, data):
LineReceiver.dataReceived(self, data)
def lineReceived(self, line):
self.factory.onLine(line)
class ChatFactory(Factory):
protocol = ChatProtocol
def __init__(self, tc, onConnectionMade, onLine):
self.tc = tc
self.onConnectionMade = onConnectionMade
self.onLine = onLine
def buildProtocol(self, addr):
p = ChatProtocol()
p.factory = self
self.onConnectionMade(p, addr)
return p
class ChatClientFactory(ClientFactory):
def __init__(self, deferred, tc, onConnectionMade):
self.tc = tc
self.deferred = deferred
self.onConnectionMade = onConnectionMade
def startedConnecting(self, connector):
self.tc.customReport('Started to connect.')
def buildProtocol(self, addr):
p = ChatProtocol()
p.factory = self
self.deferred.callback('Connected')
self.onConnectionMade(p)
return p
def clientConnectionLost(self, connector, reason):
self.tc.customReport('Lost connection. Reason: %s' % reason)
def clientConnectionFailed(self, connector, reason):
self.deferred.errback(Failure('Connection failed. Reason: %s' % reason))
def onLine(self, line):
self.tc.statusReport(line)
###############################################################################
## Classes that run in the gui thread #########################################
###############################################################################
class ChatServer(object):
"""This object is created in the gui thread
and allows for communication between the threads"""
def __init__(self, app, onStarted, onFailed, onLine, onMsg):
self.app = app
self.onStarted = onStarted
self.onFailed = onFailed
self.onLine = onLine
self.onMsg = onMsg
self.started = False
def start(self, port):
ThreadCommand((self._doStart, [port], {}),
self.onStarted, self.onFailed, onStatusReport=self.onLine,
onCustomReport=self.onMsg)
self.started = True
def stop(self, onStopped, onFail):
ThreadCommand((self._doStop, (), {}), onStopped, onFail)
self.started = False
def send(self, line):
"""Sends a line to the other end"""
return self.connection.transport.write(str(line) + '\r\n')
# This is soooo coool. Just call self.send from the gui
# and your data is sent. (it returns a deferred by the way)
send = makeNetSafe(send)
# Methods called from net thread
def _doStart(self, tc, port):
"""Starts the factory"""
self.factory = ChatFactory(tc, self._doOnConnectionMade, self._doOnLine)
self.port = reactor.listenTCP(int(port), self.factory)
def _doStop(self, tc):
"""Stops listening"""
self.port.stopListening()
def _doOnConnectionMade(self, connection, addr):
"""Receives an instance of 'ChatProtocol' for each incoming connection"""
self.connection = connection # We only really handle one connection at a time
guiCall(self.onMsg, 'Connection from %s' % addr)
def _doOnLine(self, line):
"""Passes the on line received event to the gui"""
self.factory.tc.statusReport(line)
class ChatClient(object):
"""A nice interface to be used from the gui"""
# Methods called in the gui thread
def connect(self, server, port, onSuccess, onFailure, onLine, onMsg):
ThreadCommand((self._doConnect, (server, port), {}), onSuccess, onFailure, onStatusReport=onLine, onCustomReport=onMsg)
def disconnect(self, onDone, onErr):
ThreadCommand((self._doDisconnect, (), {}), onDone, onErr)
def send(self, line):
"""Sends the line"""
return netCall(self._doSend, line)
# Methods called in the net thread
def _doConnect(self, tp, server, port):
d = Deferred()
self.factory = ChatClientFactory(d, tp, self.onConnectionMade)
self.connector = reactor.connectTCP(server, int(port), self.factory)
return d
def _doDisconnect(self, tp):
return self.connector.disconnect()
def _doSend(self, line):
self.connection.transport.write(str(line) + '\r\n')
def onConnectionMade(self, connection):
"""Called once we have connected."""
self.connection = connection
class ChatFrame(ChatFrameGui):
def __init__(self):
ChatFrameGui.__init__(self, None, -1, 'Chat')
self._assignEvents()
self.server = ChatServer(wx.GetApp(), self.onServerStarted, self.onServerFailed, self.onLine, self.onMsg)
self.client = ChatClient()
def _assignEvents(self):
eventManager.Register(self.onListen, wx.EVT_TOGGLEBUTTON, self.btnListen)
eventManager.Register(self.onConnect, wx.EVT_TOGGLEBUTTON, self.btnConnect)
eventManager.Register(lambda e: self.Close(), wx.EVT_BUTTON, self.btnClose)
eventManager.Register(self.onSend, wx.EVT_BUTTON, self.btnSend)
# Event handlers for gui framework
def onListen(self, evt):
"""Starts or stops listening"""
if evt.Checked():
self.server.start(self.spnListenPort.GetValue())
else:
self.server.stop(self.onServerStopped, self.onServerFailed)
def onConnect(self, evt):
"""Called from connect button. Connects to a server"""
if evt.Checked():
self.client.connect(self.edtIPAddress.GetValue(), self.spnConnectPort.GetValue(),
self.onClientConnected, self.onClientFailed, self.onLine,
self.onClientMsg)
else:
self.client.disconnect(lambda tc: self.onClientMsg('Client disconnected'), self.onClientFailed)
def onSend(self, evt):
"""Sends the stuff in edtToSend"""
line = self.edtToSend.GetValue()
if not line: return
if self.server.started:
d = self.server.send(line)
else:
d = self.client.send(line)
d.addCallback(self.onSent, line)
d.addErrback(self.onSendFailed, line)
self.edtToSend.SetValue('')
self.edtToSend.SetFocus()
def onSent(self, result, line):
"""Called once some text has been succesfully sent"""
self.edtSent.AppendText(line + '\n')
def onSendFailed(self, reason, line):
"""Called if a send failed"""
wx.MessageBox(str(reason), 'Could not send %s' % line, wx.OK|wx.ICON_ERROR, self)
# Event handlers for net framework
def onServerStarted(self, server):
"""Called once the server has started listening"""
self.GetStatusBar().SetStatusText('Server started')
def onServerStopped(self, server):
"""The server has been stopped by the user"""
self.GetStatusBar().SetStatusText('Server stopped')
def onServerFailed(self, reason):
"""Called if the server can't listen"""
self.btnListen.SetValue(False)
wx.MessageBox(reason, 'Server Failed', wx.OK|wx.ICON_ERROR, self)
self.GetStatusBar().SetStatusText('Server failed: %s' % reason)
def onClientConnected(self, c):
self.GetStatusBar().SetStatusText('Client Connected')
def onClientFailed(self, reason):
self.btnConnect.SetValue(False)
wx.MessageBox(str(reason), 'Client Connection Failed', wx.OK|wx.ICON_ERROR, self)
self.GetStatusBar().SetStatusText('Client Connection Failed: %s' % reason)
def onClientMsg(self, msg, extra=None):
if isinstance(msg, Failure):
msg = msg.getErrorMessage()
self.GetStatusBar().SetStatusText(msg)
def onLine(self, line):
"""Called when a line is received from the other end"""
self.edtReceived.AppendText(line + '\n')
def onMsg(self, msg):
"""Called when the server has a message for us"""
self.GetStatusBar().SetStatusText(msg)
class App(wx.App):
def OnInit(self):
self.twistedThread = TwistedThread(self, 'twistd.log')
#self.twistedThread = TwistedThread(self)
from twisted.internet.defer import Deferred
Deferred.debug = 1
self.main = ChatFrame()
eventManager.Register(self.onClose, wx.EVT_CLOSE, self.main)
self.main.Show()
return True
def onClose(self, evt):
"""Stops the twisted threads"""
self.twistedThread.stop()
evt.Skip()
if __name__ == '__main__':
a = App(0)
a.MainLoop()
|
wx runs in the main thread and starts twisted reactor in a secondary thread. Methods are called cross thread by posting messages/events on each main loop's event queue. wx uses wxEvents and twisted uses CallFromThread (thanks again to itamar and
twisted (irc.freenode.net).
We also supply a little framework for inter-thread communication. The easiest way to use it is to have your twisted object use makeNetSafe (meaning make this net thread func safe to be called from the gui thread). Use like this:
class SomeTwistedThing(Protocol):
def doSomeNetThing(self):
d = self.transport.write(self.somedata)
return d
doSomeNetThing = makeNetSafe(doSomeNetThing)
Note the indentation, we are replacing the class function with one that can be called from either the gui thread or the net thread (if a TwistedThread instance doesn't exist), it still returns a deferred and you can add callbacks and everything. Deferreds running in the gui thread don't support setTimeOut (as that relies on the reactor); you can make your own using a wxTimer or something if you need it.
If netCall and guiCall use maybeDeferred, so if the function just returns a result, it will be hopped to the other thread, if the function raises an exception, the errBack will be called (in the other thread).
Let me share one thing about deferreds that I learned while writing this. If the function fails and your errback is called, the other errbacks wont chain unless you return a Failure instance. If your errback handler doesn't return a Failure instance, the next thing to be called in the chain will be a callback.
Originally the code was made for uploading and downloading stuff, hence the StatusReport, ProgressReport, etc methods. Feel free to use them if you want too.
May God bless you and your family as you try to please Him.
Don't be scared. Don't be scared off by the size. There's a whole demo app in there.
Look for -------------------- 8< ------------------
for between file cuts...
Comments appreciated
guinet module? I'm unable to locate information on the guinet module... am I missing something?
guinet.py. Sorry, there are like three or four files in the recipe. The first file is guinet.py.
Each file is separated with a line break: -----------------8<------------------------
I've update guinet.py.
(comment continued...)
(...continued from previous comment)
(comment continued...)
(...continued from previous comment)
(comment continued...)
(...continued from previous comment)
(comment continued...)
(...continued from previous comment)