Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Replace <code>reactor.spawnProcess(proto, addr)</code> with <code>reactor.spawnProcess(proto, addr, ['maze'], {})</code>.</p> <p>Past experience has show that if you don't pass exe name as the first argument then nothing useful happens. However I have yet to find a reasonable explanation for why this happens.</p> <p>Also you don't need <code>global reactor</code>. When you import the <code>reactor</code> you add it to the top level script namespace. This means that all functions and class in the same file can use it without declaring a global or importing again.</p> <p>Also, you should not be using <code>sleep(0.01)</code> because:</p> <ol> <li>Its not a builtin function. You need to import it from the <code>time</code> <a href="http://docs.python.org/library/time.html#time.sleep" rel="nofollow">module</a>.</li> <li>Twisted is a asynchronous framework where function should avoid blocking at all costs, and <code>time.sleep()</code> (<a href="http://docs.python.org/library/time.html#time.sleep" rel="nofollow">link</a>) by its definition is a blocking call.</li> </ol> <p>You should instead use <code>reactor.callLater()</code> <a href="http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IReactorTime.html#callLater" rel="nofollow">link</a> where you provide it will a callback and a time period. This will let twisted handle other things (like a new connection) while you wait.</p> <p>Finally, you code at the moment would require the user to enter input before the program asks for any. This is because <code>getWrit</code> just sends stuff already in the buffer rather than asking the user. This means that if the user hasn't sent any data before <code>getWrit</code> is called then it will just return an empty string.</p> <p>It would be a better idea if you used a deferred. Then what you would do is call <code>getWrit</code> which would immanently return a deferred and clear the data buffer. Then in <code>dataReceived</code> you would append data to the buffer until you got a newline character (<code>\n</code>). At which point you would call the deferred set up in <code>getWrit</code>.</p> <p>Something like this:</p> <pre><code>print 'Preall test works!' from twisted.internet import reactor, protocol, defer from twisted.python import log import sys print 'Imports done' class PrgShell(protocol.Protocol): data = '' class PrgProto(protocol.ProcessProtocol): def __init__(self, out): print 'Prgproto instance made' self.transportout = out.transport self.out = out def outReceived(self, data): """Called when process sends data. We send it on to transport, however if it's 'I want input', we need to activate input.""" print 'Sub said: '+data if data == "input": print 'Sub wants input' self.transportout.write("input") d = self.out.getWrit() # getWrit returns a deferred. We store it in d to make the code more readable d.addCallback(self.sendInput) # Here we add self.sendInput to the callback chain. # This way self.sendInput gets called with the user input. else: self.transportout.write(data) def sendInput(self, data): self.transport.write(data) def getWrit(self): print 'Proto gave input to prg' self.deferred = defer.deferred() self.data = '' return self.deferred def connectionMade(self): print 'Connected' proto = self.PrgProto(self) addr = "C:\\Documents and Settings\\papa\\My Documents\\Python\\Files\\Maze\\exe\\maze.exe" reactor.spawnProcess(proto, addr, ['maze'], {}) print 'Procces spawned!' def dataReceived(self, data): print 'Data recived: '+data self.data+=data if self.data.endswith('\n'): if self.deferred: # We got a newline character, and there is a deferred to call, so lets call it d, self.deferred = self.deferred, None # This will set self.deferred to none to stop mistakes later d.callback(self.data) # Call the deferred with data. This will send the data to sendInput above. self.data = '' # Clear the buffer print 'About to do stuff' factory = protocol.ServerFactory() factory.protocol = PrgShell #f = open("errors.txt", 'w') #log.startLogging(f) #print 'Logging started' reactor.listenTCP(8000,factory) print 'Runing' reactor.run() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload