Example #1
0
def step(sleep = True, external = True):
    if reactor.running:
        try:
            t = sleep and reactor.running and reactor.timeout()
            reactor.doIteration(t)
            reactor.runUntilCurrent()
        except:
            log.error("problem running reactor - exiting")
            raise SystemExit
        if external:
            dispatch.dispatcher_run()
    else:
        log.info("reactor stopped - exiting")
        raise SystemExit
Example #2
0
def step(sleep=True, external=True):
    if reactor.running:
        try:
            t = sleep and reactor.running and reactor.timeout()
            reactor.doIteration(t)
            reactor.runUntilCurrent()
        except:
            log.error("problem running reactor - exiting")
            raise SystemExit
        if external:
            dispatch.dispatcher_run()
    else:
        log.info("reactor stopped - exiting")
        raise SystemExit
Example #3
0
def step( sleep = True, external = True ):
	global __min_timer

	if __min_timer and sleep:
		time = qt.QTime()
		time.start()
		qt.QCoreApplication.processEvents( qt.QEventLoop.AllEvents | qt.QEventLoop.WaitForMoreEvents,
									   __min_timer )
		if time.elapsed() < __min_timer:
			qt.QThread.usleep( __min_timer - time.elapsed() )
	else:
		qt.QCoreApplication.processEvents( qt.QEventLoop.AllEvents | qt.QEventLoop.WaitForMoreEvents )

	if external:
		dispatch.dispatcher_run()
Example #4
0
def step(sleep=True, external=True):
    global __min_timer

    if __min_timer and sleep:
        time = qt.QTime()
        time.start()
        qt.QCoreApplication.processEvents(
            qt.QEventLoop.AllEvents | qt.QEventLoop.WaitForMoreEvents,
            __min_timer)
        if time.elapsed() < __min_timer:
            qt.QThread.usleep(__min_timer - time.elapsed())
    else:
        qt.QCoreApplication.processEvents(qt.QEventLoop.AllEvents
                                          | qt.QEventLoop.WaitForMoreEvents)

    if external:
        dispatch.dispatcher_run()
Example #5
0
def step( sleep = True, external = True, simulate = False ):
	"""Do one step forward in the main loop. First all timers are checked for
	expiration and if necessary the accociated callback function is called.
	After that the timer list is searched for the next timer that will expire.
	This will define the maximum timeout for the following select statement
	evaluating the registered sockets. Returning from the select statement the
	callback functions from the sockets reported by the select system call are
	invoked. As a final task in a notifier step all registered external
	dispatcher functions are invoked."""

	global __in_step, __step_depth, __step_depth_max

	__in_step = True
	__step_depth += 1

	try:
		if __step_depth > __step_depth_max:
			log.exception( 'maximum recursion depth reached' )
			return

		# get minInterval for max timeout
		timeout = None
		if not sleep:
			timeout = 0
		else:
			now = int( time() * 1000 )
			for interval, timestamp, callback in __timers.values():
				if not timestamp:
					# timer is blocked (recursion), ignore it
					continue
				nextCall = timestamp - now
				if timeout == None or nextCall < timeout:
					if nextCall > 0:
						timeout = nextCall
					else:
						timeout = 0
						break
			if timeout == None:
				if dispatch.dispatcher_count():
					timeout = dispatch.MIN_TIMER
				else:
					# No timers and no dispatchers, timeout could be infinity.
					timeout = 30000
			if __min_timer and __min_timer < timeout: timeout = __min_timer

		# wait for event
		r = w = e = ()
		try:
			if timeout:
				timeout /= 1000.0
			r, w, e = select( __sockets[ IO_READ ].keys(), __sockets[ IO_WRITE ].keys(),
							  __sockets[ IO_EXCEPT ].keys(), timeout )
		except select_error, e:
			if e[ 0 ] != errno.EINTR:
				raise e

		if simulate:
			# we only simulate
			return
		
		# handle timers
		for i, timer in __timers.items():
			timestamp = timer[ TIMESTAMP ]
			if not timestamp:
				# prevent recursion, ignore this timer
				continue
			now = int( time() * 1000 )
			if timestamp <= now:
				# Update timestamp on timer before calling the callback to
				# prevent infinite recursion in case the callback calls
				# step().
				timer[ TIMESTAMP ] = 0
				if not timer[ CALLBACK ]():
					if __timers.has_key( i ):
						del __timers[ i ]
				else:
					# Find a moment in the future. If interval is 0, we
					# just reuse the old timestamp, doesn't matter.
					now = int( time() * 1000 )
					if timer[ INTERVAL ]:
						timestamp += timer[ INTERVAL ]
						while timestamp <= now:
							timestamp += timer[ INTERVAL ]
					timer[ TIMESTAMP ] = timestamp

		# handle sockets
		for sl in ( ( r, IO_READ ), ( w, IO_WRITE ), ( e, IO_EXCEPT ) ):
			sockets, condition = sl
			# append all unknown sockets to check list
			for s in sockets:
				if not s in __current_sockets[ condition ]:
					__current_sockets[ condition ].append( s )
			while len( __current_sockets[ condition ] ):
				sock = __current_sockets[ condition ].pop( 0 )
				is_socket = isinstance( sock, ( socket.socket, file, socket._socketobject ) )
				if ( is_socket and (getattr(sock, 'closed', False) or sock.fileno() != -1 )) or \
					   ( isinstance( sock, int ) and sock != -1 ):
					if __sockets[ condition ].has_key( sock ) and \
						   not __sockets[ condition ][ sock ]( sock ):
						socket_remove( sock, condition )

		# handle external dispatchers
		if external:
			dispatch.dispatcher_run()
Example #6
0
				if condition in ( select.POLLHUP, select.POLLNVAL ):
					socket_remove( sock_obj, IO_ALL )
					continue
				# check for errors
				if condition == select.POLLERR:
					if fd in __sockets[ IO_EXCEPT ] and not __sockets[ IO_EXCEPT ][ fd ]( sock_obj ):
						socket_remove( sock_obj, IO_EXCEPT )
					continue
				for cond in ( IO_READ, IO_WRITE ):
					if cond & condition and fd in __sockets[ cond ] and \
						   not __sockets[ cond ][ fd ]( sock_obj ):
						socket_remove( sock_obj, cond )

		# handle external dispatchers
		if external:
			__min_timer = dispatch.dispatcher_run()
	finally:
		__step_depth -= 1
		__in_step = False

def loop():
	"""Executes the 'main loop' forever by calling step in an endless loop"""
	while True:
		step()

def _init():
	global __step_depth_max

	log.set_level( log.CRITICAL )
	__step_depth_max = _options[ 'recursive_depth' ]
Example #7
0
                    socket_remove(sock_obj, IO_ALL)
                    continue
                # check for errors
                if condition == select.POLLERR:
                    if fd in __sockets[IO_EXCEPT] and not __sockets[IO_EXCEPT][
                            fd](sock_obj):
                        socket_remove(sock_obj, IO_EXCEPT)
                    continue
                for cond in (IO_READ, IO_WRITE):
                    if cond & condition and fd in __sockets[ cond ] and \
                        not __sockets[ cond ][ fd ]( sock_obj ):
                        socket_remove(sock_obj, cond)

        # handle external dispatchers
        if external:
            __min_timer = dispatch.dispatcher_run()
    finally:
        __step_depth -= 1
        __in_step = False


def loop():
    """Executes the 'main loop' forever by calling step in an endless loop"""
    while True:
        step()


def _init():
    global __step_depth_max

    log.set_level(log.CRITICAL)
Example #8
0
def step( sleep = True, external = True ):
	global _step
	_step( sleep )
	if external:
		dispatch.dispatcher_run()