Because I want the client every 200ms synchronization server message. The
server provides the client calls the command, this is just my idea, it can
work, for example, when I add a project on the server, or delete a project
I want my client to get the server instant message.
在 2012年8月17日星期五UTC+8下午12时37分08秒,seeks写道:
>
> Hello, everyone, I have a twisted use in python. The red part of the
> problem in the following code.
> I want a process a call to the remote server command, but I failed, I can
> not find the reason, can help?
>
>
> import sys,time
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> from net.client import FilmStudioClient
> from twisted.python import log
>
> def DefaultErrorHandler(exc):
> msg = '%s\n\n%s' % (exc.getErrorMessage(), str(exc))
> log.msg(msg)
>
> class Window(QWidget):
> a = 0
> def __init__(self,reactor, parent = None):
>
> QWidget.__init__(self, parent)
> self.projects = []
> self.reactor = reactor
> button = QPushButton(self.tr("Click me"))
> self.resultLabel = QLabel(self.tr("..."))
> self.client = FilmStudioClient(reactor,self.onDisconnected)
> # New style: uses the connect method of a pyqtSignal object.
> self.connect(button, SIGNAL("clicked()"), self.handleClick)
>
> # Old style: uses the SIGNAL function to describe the signal.
> self.connect(self, SIGNAL("sendValue(PyQt_PyObject)"),
> self.handleValue)
>
> layout = QVBoxLayout(self)
> layout.addWidget(button)
> layout.addWidget(self.resultLabel)
>
> self.threadPool = []
>
> def connectAnonymous(self):
>
> self.client.connectAnonymous("192.168.0.108",7777).addCallback(self.onConnected).addErrback(
>
> DefaultErrorHandler )
> def handleClick(self):
> #self.connectAnonymous()
> self.threadPool.append( GenericThread(self.getP) )
> self.disconnect( self, SIGNAL("sendValue(PyQt_PyObject)"),
> self.handleValue )
> self.connect( self, SIGNAL("sendValue(PyQt_PyObject)"),
> self.handleValue )
> self.threadPool[len(self.threadPool)-1].start()
>
> def getP(self):
> while True:
> time.sleep(1)
> #self.connectAnonymous()
> # i can not get projects from QThread because getProjects_OK
> was not run.
> self.client.perspective.callRemote(
> 'getProjects').addCallback(self.getProjects_OK)
>
> #self.client.getProjects().addCallback(self.getProjects_OK).addErrback(self.getProjects_Failed)
> self.emit(SIGNAL("sendValue(PyQt_PyObject)"), self.projects)
>
> def handleValue(self, value):
> self.resultLabel.setText(repr(value))
>
> def onConnected(self, perspective):
> self.connected = True
> def onDisconnected(self):
> self.connected = False
>
> def getProjects_OK(self,info):
> print "get-ok",info,"done"
> self.projects = []
> for i in info:
> print "project:",i
> self.projects.append(i.name)
> def getProjects_Failed(self,exc):
> print "getProjects_Failed",exc
>
>
> class GenericThread(QThread):
> def __init__(self, function, *args, **kwargs):
> QThread.__init__(self)
> self.function = function
> self.args = args
> self.kwargs = kwargs
>
> def __del__(self):
> self.wait()
>
> def run(self):
> self.function(*self.args,**self.kwargs)
> return
>
> def main():
>
> import sys
> try:
> import qt4reactor
> except ImportError:
> # Maybe qt4reactor is placed inside twisted.internet in
> site-packages?
> from twisted.internet import qt4reactor
> qt4reactor.install()
>
> from twisted.internet import reactor
>
> app = QApplication(sys.argv)
> app.setStyle(QStyleFactory.create("Plastique"))
> win = Window(reactor)
> win.connectAnonymous()
> win.show()
>
>
>
> reactor.run()
>
>
--
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings:
http://groups.google.com/group/python_inside_maya/subscribe