There are a couple of red flags here. One is the use of "while True". The other is the use of time.sleep.

It seems that you may want to emit the signal in your callback method - until that method is called the "self.projects" attribute will not be (correctly) populated.

Something like this:


    def getP(self):
        df = self.client.perspective.callRemote('getProjects')
        df.addCallback(self.getProjects_OK)
        df.addErrback(self.getProjects_Failed)
        """
        Note: we don't have the result of 'getProjects' yet.
        """
        return df


    def getProjects_OK(self,info):
        """
        The result of 'getProjects' has arrived.
        """
        print "get-ok",info,"done"
        self.projects = []

        for i in info:
            print "project:",i
            self.projects.append(i.name)

        self.emit(SIGNAL("sendValue(PyQt_PyObject)"), self.projects)

    def getProjects_Failed(self,exc):
        print "getProjects_Failed",exc


I wonder which version of the reactor you're using? I imagine you want to use the qtreactor?

I'm not clear on what you wanted to use 'time.sleep' for... if you need to introduce a delay while some other step completes, it might be better to have a look at using reactor.callLater.

I hope that's helpful.

Donal


On 17/08/12 16:37, seeks wrote:
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


--

Donal McMullan
Production Engineer


--
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to