Exemple #1
0
    def readlinesAndWait(self):
        from twisted.internet import reactor
        self.isRunning = True
        Topen.activePool.append(self)

        self.finished.acquire()
        from Hellanzb.Log import debug
        import thread
        debug('spawnProcess THREAD ID: ' + str(thread.get_ident()) + ' (' + \
                  self.prettyCmd + ')')

        # The reactor could have fallen asleep on us in -Lp mode! Why? I'm not sure, but
        # it dies after the first par2 process (Topen call) =[
        reactor.wakeUp()

        # Have the main, twisted thread, run the process. If we trigger spawnProcess from
        # a separate thread (which PostProcessor/Topens are always used from) instead, bad
        # things can occur (on Linux 2.6.x we can end up with defunct processes -- Trac
        # ticket #33). Running any twisted call from a non twisted thread is asking for
        # trouble. We also MUST usePTY, otherwise the processes receive signals (in
        # particular, SIGINT, rendering our first CTRL-C ignoring code useless, as it ends
        # up killing our sub processes)
        reactor.callFromThread(reactor.spawnProcess, self, self.cmd[0], self.cmd, os.environ,
                               usePTY = (not isWindows() and not isSolaris()) and 1 or 0)

        self.finished.wait()
        self.finished.release()

        # Here is where PostProcessor will typically die. After a process has been killed
        checkShutdown()

        # prepare the outbuffer (LAME)
        output = [line + '\n' for line in self.outBuf.getvalue().split('\n')]
        
        return output, self.returnCode
 def stop(self, failed=False, *args, **kw):
     from twisted.internet import reactor
     if failed:
         reactor.callLater(0.00001, self.failed, *args, **kw)
     else:
         reactor.callLater(0.00001, self.done, *args, **kw)
     reactor.wakeUp()
Exemple #3
0
def blockingCallOnMainThread(func, *args, **kwargs):
	"""
	  Modified version of twisted.internet.threads.blockingCallFromThread
	  which waits 30s for results and otherwise assumes the system to be shut down.
	  This is an ugly workaround for a twisted-internal deadlock.
	  Please keep the look intact in case someone comes up with a way
	  to reliably detect from the outside if twisted is currently shutting
	  down.
	"""
	def blockingCallFromThread(f, *a, **kw):
		queue = Queue.Queue()
		def _callFromThread():
			result = defer.maybeDeferred(f, *a, **kw)
			result.addBoth(queue.put)
		reactor.callFromThread(_callFromThread)

		result = None
		while True:
			try:
				result = queue.get(True, 30)
			except Queue.Empty as qe:
				if True: #not reactor.running: # reactor.running is only False AFTER shutdown, we are during.
					raise ValueError("Reactor no longer active, aborting.")
			else:
				break

		if isinstance(result, failure.Failure):
			result.raiseException()
		return result

	if currentThread().getName() == 'MainThread':
		return func(*args, **kwargs)
	else:
		return blockingCallFromThread(func, *args, **kwargs)
	reactor.wakeUp()
Exemple #4
0
 def __send_report(self, report):
     """Send a report to the clients"""
     message = emuML.write_report(report)
     for client in self.factory.clients:
         logging.info(_("sending report {0}").format(message))
         client.sendLine(message)
         reactor.wakeUp()
Exemple #5
0
 def get_dataport(self):
     while self.dataport is None:
         #print "DEBUG: Waiting for dataport"
         #reactor.callFromThread(time.sleep, 0.5)
         reactor.wakeUp()
         time.sleep(0.5)
     return self.dataport
Exemple #6
0
			def on_finish(success,result):
				sys.stderr.write("at leat the thread finished\n")
				if success:
					sys.stderr.write("success\n")
					reactor.wakeUp()
					
					reactor.callFromThread(deferred.callback,result)
				else:
					sys.stderr.write("fail\n")
					reactor.callFromThread(deferred.errback,result)
Exemple #7
0
            def on_finish(success, result):
                sys.stderr.write("at leat the thread finished\n")
                if success:
                    sys.stderr.write("success\n")
                    reactor.wakeUp()

                    reactor.callFromThread(deferred.callback, result)
                else:
                    sys.stderr.write("fail\n")
                    reactor.callFromThread(deferred.errback, result)
Exemple #8
0
def callOnMainThread(func, *args, **kwargs):
    """
	  Ensures that a method is being called on the main-thread.
	  No return value here!
	"""
    if currentThread().getName() == 'MainThread':
        #call on next mainloop interation
        reactor.callLater(0, func, *args, **kwargs)
    else:
        #call on mainthread
        reactor.callFromThread(func, *args, **kwargs)
    reactor.wakeUp()
Exemple #9
0
def callOnMainThread(func, *args, **kwargs):
	"""
	  Ensures that a method is being called on the main-thread.
	  No return value here!
	"""
	if currentThread().getName() == 'MainThread':
		#call on next mainloop interation
		reactor.callLater(0, func, *args, **kwargs)
	else:
		#call on mainthread
		reactor.callFromThread(func, *args, **kwargs)
	reactor.wakeUp()
Exemple #10
0
 def handleSNOTLogLine(line):
     print line,
     regex = r"^(?P<date>.+?) CMD: (?P<cmd>\w+?) TKT: (?P<tkt>\d+?) BY: (?P<by>.+?)($| TO: (?P<to>.+))?$"
     match = re.match(regex, line)
     if match:
         try:
             mdict = match.groupdict()
             message = str(mdict)
             ticketDict = sp.parseTicket(int(mdict['tkt']))
             formattedTicket = sp.formatTicketDictSmart(ticketDict, config['snot']['formatString'])
             # "Case" statement for various ticket commands
             cmd = mdict["cmd"].lower()
             if cmd == "flags":
                 message = "#{tkt} (\"" + ticketDict['subject'] + "\") flagged as {to} by {by}"
                 message = message.format(**mdict)
                 if mdict['to'] in config['snot']['alerts']['flag']:
                     for target in config['snot']['alerts']['flag'][mdict['to']]:
                         client.notice(target, "Flagged as %s: %s" % (mdict['to'], formattedTicket))
                         client.logger.write("SNOTMagic: Message '%s' sent to %s" % (formattedTicket, string.join(config['snot']['alerts']['flag'][mdict['to']], ", ")) )
                 reactor.wakeUp()
             elif cmd == "recv":
                 client.msg(config['snot']['snot_channel'], "Received ticket #{tkt} from {by}".format(**mdict))
                 client.msg(config['snot']['snot_channel'], formattedTicket)
                 return
             elif cmd == "resp":
                 message = "{by} assigned #{tkt} to {to}".format(**mdict)
             elif cmd == "complete":
                 message = "#{tkt} completed by {by}".format(**mdict)
             elif cmd == "delete":
                 message = "#{tkt} deleted by {by}".format(**mdict)
             elif cmd == "update":
                 message = "#{tkt} (\"" + ticketDict['subject'] + "\") updated by {by}"
                 message = message.format(**mdict)
             elif cmd == "append":
                 message = "#{tkt} appended to #{to} by {by}".format(**mdict)
             elif cmd == "autoresolve":
                 message = "#{tkt} autoresolved - ({by})".format(**mdict)
             elif cmd == "priority" or cmd == "autopriority":
                 message = "#{tkt} priority set to {to} by {by}".format(**mdict)
             else:
                 message = line
             #client.logger.write("SNOTMagic: Message '%s' sent to %s" % (message, config['snot']['snot_channel']))
             client.msg(config['snot']['snot_channel'], message)
         except KeyError as ke:
             client.logger.write(str(ke))
             client.msg(config['snot']['snot_channel'], str(ke))
         except TypeError as te:
             client.logger.write(str(te))
             client.msg(config['snot']['snot_channel'], str(te))
     else:
         client.msg("#snot", "Could not match '%s'" % line)
     reactor.wakeUp()
 def _pollUsb(self):
     """ Since select does not work on the 
         Android UsbDevice, manually read and write.
     """
     if not hasattr(self,'_serial'):
         return # Not yet ready...
     from twisted.internet import reactor
     #: Do any pending io
     log.warning("_pollUsb..")
     reactor._sendToThread(self._ioThread)
     reactor.wakeUp()
     #: Force rw
     reactor._process_Notify([self],[self])
Exemple #12
0
 def connectionLost(self, reason):
     self.disconnected = True
     if self.error:
         self.error(self, reason)
     # trampoline() will now throw() into the greenlet that owns the socket
     # leaving the mainloop unscheduled. However, when the next switch to
     # the mainloop occurs, twisted will not re-evaluate the delayed calls
     # because it assumes that none were scheduled since no client code was
     # executed (it has no idea it was switched away). Because of this we
     # need to explicitly restart the mainloop.
     # Note: epollreactor still doesn't work.
     from twisted.internet import reactor
     reactor.wakeUp()
Exemple #13
0
 def on_workflow_finished(self):
     if threading.current_thread().ident == self._reactor_thread_ident:
         reactor.callWhenRunning(self.stop)
         return
     reactor.callFromThread(self.stop)
     self.debug("%s signalled that it had finished, enqueued self.stop",
                self.workflow)
     # Sometimes, reactor does not wake up from the first attempt
     # (inside callFromThread). This looks absurd, but it's true.
     # os.fsync on reactor.waker.o does not help (not a buffering issue?).
     while self._running:
         self.debug("wake up, Neo")
         reactor.wakeUp()
Exemple #14
0
 def on_workflow_finished(self):
     if threading.current_thread().ident == self._reactor_thread_ident:
         reactor.callWhenRunning(self.stop)
         return
     reactor.callFromThread(self.stop)
     self.debug("%s signalled that it had finished, enqueued self.stop",
                self.workflow)
     # Sometimes, reactor does not wake up from the first attempt
     # (inside callFromThread). This looks absurd, but it's true.
     # os.fsync on reactor.waker.o does not help (not a buffering issue?).
     while self._running:
         self.debug("wake up, Neo")
         reactor.wakeUp()
Exemple #15
0
 def tearDown(self):
     try:
         for node in self.nodes:
             node.unload()
         internet.clear()
     finally:
         while self._tempdirs:
             shutil.rmtree(self._tempdirs.pop(), ignore_errors=True)
     super(TestBase, self).tearDown()
     # After the super tearDown the remaining blocking calls should have been cancelled.
     # We reschedule the reactor to inspect itself after 0.01 seconds. Note that this cannot
     # be done after 0 seconds because we need to exit this Deferred callback chain first.
     # Also note that the longer we make this timeout, the longer we will have to wait before
     # we can cleanup.
     shutdown_dc = deferLater(reactor, 0.01, lambda: None)
     reactor.wakeUp()
     return shutdown_dc
  def sendNext(self, stateNumber):
    # Fetch the next agent
    if self.agentIndex % 2 == 0:
      agent = self.p1
    else:
      agent = self.p2 
    self.game.state.data._agentMoved = (self.agentIndex - 1) % self.numAgents
    self.game.display.update( self.game.state.data )

    observation = self.game.state.makeObservation(self.agentIndex)
    if len(self.game.moveHistory) > 10:
      observation.data.layout = None
    observation.data.food = observation.data.food.packBits()
     
    agent.protocol.sendLine("501:" + str(self.agentIndex) + ":Your Turn!")
    agent.protocol.sendLine("502:BEGIN PICKLE")
    thepickle = cPickle.dumps((stateNumber, observation), 1)
    agent.protocol.sendLine(thepickle)
    agent.protocol.sendLine("\r\n511: EOP (End of Pickle)")
    reactor.wakeUp()
    self.startTimeoutTimer(stateNumber)
Exemple #17
0
    def readlinesAndWait(self):
        from twisted.internet import reactor
        self.isRunning = True
        Topen.activePool.append(self)

        self.finished.acquire()
        from Hellanzb.Log import debug
        import thread
        debug('spawnProcess THREAD ID: ' + str(thread.get_ident()) + ' (' + \
                  self.prettyCmd + ')')

        # The reactor could have fallen asleep on us in -Lp mode! Why? I'm not sure, but
        # it dies after the first par2 process (Topen call) =[
        reactor.wakeUp()

        # Have the main, twisted thread, run the process. If we trigger spawnProcess from
        # a separate thread (which PostProcessor/Topens are always used from) instead, bad
        # things can occur (on Linux 2.6.x we can end up with defunct processes -- Trac
        # ticket #33). Running any twisted call from a non twisted thread is asking for
        # trouble. We also MUST usePTY, otherwise the processes receive signals (in
        # particular, SIGINT, rendering our first CTRL-C ignoring code useless, as it ends
        # up killing our sub processes)
        reactor.callFromThread(
            reactor.spawnProcess,
            self,
            self.cmd[0],
            self.cmd,
            os.environ,
            usePTY=(not isWindows() and not isSolaris()) and 1 or 0)

        self.finished.wait()
        self.finished.release()

        # Here is where PostProcessor will typically die. After a process has been killed
        checkShutdown()

        # prepare the outbuffer (LAME)
        output = [line + '\n' for line in self.outBuf.getvalue().split('\n')]

        return output, self.returnCode
Exemple #18
0
def blockingCallOnMainThread(func, *args, **kwargs):
    """
	  Modified version of twisted.internet.threads.blockingCallFromThread
	  which waits 30s for results and otherwise assumes the system to be shut down.
	  This is an ugly workaround for a twisted-internal deadlock.
	  Please keep the look intact in case someone comes up with a way
	  to reliably detect from the outside if twisted is currently shutting
	  down.
	"""
    def blockingCallFromThread(f, *a, **kw):
        queue = Queue.Queue()

        def _callFromThread():
            result = defer.maybeDeferred(f, *a, **kw)
            result.addBoth(queue.put)

        reactor.callFromThread(_callFromThread)

        result = None
        while True:
            try:
                result = queue.get(True, 30)
            except Queue.Empty as qe:
                if True:  #not reactor.running: # reactor.running is only False AFTER shutdown, we are during.
                    raise ValueError("Reactor no longer active, aborting.")
            else:
                break

        if isinstance(result, failure.Failure):
            result.raiseException()
        return result

    if currentThread().getName() == 'MainThread':
        return func(*args, **kwargs)
    else:
        return blockingCallFromThread(func, *args, **kwargs)
    reactor.wakeUp()
Exemple #19
0
 def wake(reactor=reactor):
     time.sleep(0.5)
     reactor.wakeUp()
 def sendRequest(self, request):
     #TODO: add request to list, notify
     line = emuML.write_request(request)
     ClientFactory.protocol.sendLine(line)
     reactor.wakeUp()