예제 #1
0
def select(space, w_iwtd, w_owtd, w_ewtd, w_timeout=None):
    """Wait until one or more file descriptors are ready for some kind of I/O.
The first three arguments are sequences of file descriptors to be waited for:
rlist -- wait until ready for reading
wlist -- wait until ready for writing
xlist -- wait for an ``exceptional condition''
If only one kind of condition is required, pass [] for the other lists.
A file descriptor is either a socket or file object, or a small integer
gotten from a fileno() method call on one of those.

The optional 4th argument specifies a timeout in seconds; it may be
a floating point number to specify fractions of seconds.  If it is absent
or None, the call will never time out.

The return value is a tuple of three lists corresponding to the first three
arguments; each contains the subset of the corresponding file descriptors
that are ready.

*** IMPORTANT NOTICE ***
On Windows, only sockets are supported; on Unix, all file descriptors.
"""

    iwtd_w = space.listview(w_iwtd)
    owtd_w = space.listview(w_owtd)
    ewtd_w = space.listview(w_ewtd)
    iwtd = [as_fd_w(space, w_f) for w_f in iwtd_w]
    owtd = [as_fd_w(space, w_f) for w_f in owtd_w]
    ewtd = [as_fd_w(space, w_f) for w_f in ewtd_w]
    iwtd_d = {}
    owtd_d = {}
    ewtd_d = {}
    for i in range(len(iwtd)):
        iwtd_d[iwtd[i]] = iwtd_w[i]
    for i in range(len(owtd)):
        owtd_d[owtd[i]] = owtd_w[i]
    for i in range(len(ewtd)):
        ewtd_d[ewtd[i]] = ewtd_w[i]
    try:
        if space.is_w(w_timeout, space.w_None):
            iwtd, owtd, ewtd = rpoll.select(iwtd, owtd, ewtd)
        else:
            iwtd, owtd, ewtd = rpoll.select(iwtd, owtd, ewtd,
                                            space.float_w(w_timeout))
    except rpoll.SelectError, s:
        w_module = space.getbuiltinmodule('select')
        w_errortype = space.getattr(w_module, space.wrap('error'))
        raise OperationError(
            w_errortype,
            space.newtuple([space.wrap(s.errno),
                            space.wrap(s.get_msg())]))
예제 #2
0
def check_socket_and_wait_for_timeout(space, w_sock, writing):
    """If the socket has a timeout, do a select()/poll() on the socket.
    The argument writing indicates the direction.
    Returns one of the possibilities in the timeout_state enum (above)."""

    w_timeout = space.call_method(w_sock, "gettimeout")
    if space.is_w(w_timeout, space.w_None):
        return SOCKET_IS_BLOCKING
    elif space.float_w(w_timeout) == 0.0:
        return SOCKET_IS_NONBLOCKING
    sock_timeout = space.float_w(w_timeout)

    # guard against closed socket
    try:
        space.call_method(w_sock, "fileno")
    except:
        return SOCKET_HAS_BEEN_CLOSED

    sock_fd = space.int_w(space.call_method(w_sock, "fileno"))

    # see if the socket is ready

    # Prefer poll, if available, since you can poll() any fd
    # which can't be done with select().
    if HAVE_RPOLL:
        if writing:
            fddict = {sock_fd: rpoll.POLLOUT}
        else:
            fddict = {sock_fd: rpoll.POLLIN}

        # socket's timeout is in seconds, poll's timeout in ms
        timeout = int(sock_timeout * 1000 + 0.5)
        ready = rpoll.poll(fddict, timeout)
    else:
        if MAX_FD_SIZE is not None and sock_fd >= MAX_FD_SIZE:
            return SOCKET_TOO_LARGE_FOR_SELECT

        if writing:
            r, w, e = rpoll.select([], [sock_fd], [], sock_timeout)
            ready = w
        else:
            r, w, e = rpoll.select([sock_fd], [], [], sock_timeout)
            ready = r
    if ready:
        return SOCKET_OPERATION_OK
    else:
        return SOCKET_HAS_TIMED_OUT
예제 #3
0
def check_socket_and_wait_for_timeout(space, w_sock, writing):
    """If the socket has a timeout, do a select()/poll() on the socket.
    The argument writing indicates the direction.
    Returns one of the possibilities in the timeout_state enum (above)."""

    w_timeout = space.call_method(w_sock, "gettimeout")
    if space.is_w(w_timeout, space.w_None):
        return SOCKET_IS_BLOCKING
    elif space.float_w(w_timeout) == 0.0:
        return SOCKET_IS_NONBLOCKING
    sock_timeout = space.float_w(w_timeout)

    # guard against closed socket
    try:
        space.call_method(w_sock, "fileno")
    except:
        return SOCKET_HAS_BEEN_CLOSED

    sock_fd = space.int_w(space.call_method(w_sock, "fileno"))

    # see if the socket is ready

    # Prefer poll, if available, since you can poll() any fd
    # which can't be done with select().
    if HAVE_RPOLL:
        if writing:
            fddict = {sock_fd: rpoll.POLLOUT}
        else:
            fddict = {sock_fd: rpoll.POLLIN}

        # socket's timeout is in seconds, poll's timeout in ms
        timeout = int(sock_timeout * 1000 + 0.5)
        ready = rpoll.poll(fddict, timeout)
    else:
        if MAX_FD_SIZE is not None and sock_fd >= MAX_FD_SIZE:
            return SOCKET_TOO_LARGE_FOR_SELECT

        if writing:
            r, w, e = rpoll.select([], [sock_fd], [], sock_timeout)
            ready = w
        else:
            r, w, e = rpoll.select([sock_fd], [], [], sock_timeout)
            ready = r
    if ready:
        return SOCKET_OPERATION_OK
    else:
        return SOCKET_HAS_TIMED_OUT
예제 #4
0
def select(space, w_iwtd, w_owtd, w_ewtd, w_timeout=None):
    """Wait until one or more file descriptors are ready for some kind of I/O.
The first three arguments are sequences of file descriptors to be waited for:
rlist -- wait until ready for reading
wlist -- wait until ready for writing
xlist -- wait for an ``exceptional condition''
If only one kind of condition is required, pass [] for the other lists.
A file descriptor is either a socket or file object, or a small integer
gotten from a fileno() method call on one of those.

The optional 4th argument specifies a timeout in seconds; it may be
a floating point number to specify fractions of seconds.  If it is absent
or None, the call will never time out.

The return value is a tuple of three lists corresponding to the first three
arguments; each contains the subset of the corresponding file descriptors
that are ready.

*** IMPORTANT NOTICE ***
On Windows, only sockets are supported; on Unix, all file descriptors.
"""

    iwtd_w = space.listview(w_iwtd)
    owtd_w = space.listview(w_owtd)
    ewtd_w = space.listview(w_ewtd)
    iwtd = [as_fd_w(space, w_f) for w_f in iwtd_w]
    owtd = [as_fd_w(space, w_f) for w_f in owtd_w]
    ewtd = [as_fd_w(space, w_f) for w_f in ewtd_w]
    iwtd_d = {}
    owtd_d = {}
    ewtd_d = {}
    for i in range(len(iwtd)):
        iwtd_d[iwtd[i]] = iwtd_w[i]
    for i in range(len(owtd)):
        owtd_d[owtd[i]] = owtd_w[i]
    for i in range(len(ewtd)):
        ewtd_d[ewtd[i]] = ewtd_w[i]
    try:
        if space.is_w(w_timeout, space.w_None):
            iwtd, owtd, ewtd = rpoll.select(iwtd, owtd, ewtd)
        else:
            iwtd, owtd, ewtd = rpoll.select(iwtd, owtd, ewtd, space.float_w(w_timeout))
    except rpoll.SelectError, s:
        w_module = space.getbuiltinmodule('select')
        w_errortype = space.getattr(w_module, space.wrap('error'))
        raise OperationError(w_errortype, space.newtuple([
            space.wrap(s.errno), space.wrap(s.get_msg())]))
예제 #5
0
    def do_poll(self, space, timeout):
        if not self._check_fd():
            raise OperationError(space.w_IOError, space.wrap("handle out of range in select()"))

        r, w, e = rpoll.select([self.fd], [], [], timeout)
        if r:
            return True
        else:
            return False
예제 #6
0
    def do_poll(self, space, timeout):
        if not self._check_fd():
            raise OperationError(space.w_IOError,
                                 space.wrap("handle out of range in select()"))

        r, w, e = rpoll.select([self.fd], [], [], timeout)
        if r:
            return True
        else:
            return False
예제 #7
0
 def loop(self):
     self._active = True
     fmt = self.screen.c_format
     RSDL.FillRect(self.screen, lltype.nullptr(RSDL.Rect), self.ColorGrey)
     layers = self._layers
     neurons = self._neurons
     pulse_layers = self._pulse_layers
     screen = self.screen
     loops = 0
     now = start = float(time.time())
     while self._active:
         #Bjitdriver.can_enter_jit( layers=layers, neurons=neurons, pulse_layers=pulse_layers, loops=loops )
         #Bjitdriver.jit_merge_point( layers=layers, neurons=neurons, pulse_layers=pulse_layers, loops=loops)
         now = float(time.time())
         self._fps = loops / float(now - start)
         for i, lay in enumerate(self._layers):
             if self._pulse_layers[i] and False:
                 #print 'pulse layer: %s neurons: %s ' %(i, len(lay))
                 for n in lay:
                     if random() * random() > 0.8:
                         n.spike(now)
         for i, col in enumerate(self._columns):
             if self._pulse_columns[i]:
                 for n in col:
                     n.spike(now)
         for n in self._neurons:
             n.iterate()
             n.draw(self.screen)
         #r,w,x = rpoll.select( [self._stdin], [], [], 1 )	# wait
         rl, wl, xl = rpoll.select([0], [], [], 0.000001)  # wait
         if rl:
             cmd = self._stdin.readline().strip('\n').strip(' ')
             self.do_command(cmd)
         loops += 1
         self._iterations = loops
         #print loops		# can not always print in mainloop, then select can never read from stdin
         RSDL.Flip(self.screen)
         #self._fps = float(time.time()) - now
     #return loops
     return 0
	def loop( self ):
		self._active = True
		fmt = self.screen.c_format
		RSDL.FillRect(self.screen, lltype.nullptr(RSDL.Rect), self.ColorGrey)
		layers = self._layers
		neurons = self._neurons
		pulse_layers = self._pulse_layers
		screen = self.screen
		loops = 0	
		now = start = float( time.time() )
		while self._active:
			#Bjitdriver.can_enter_jit( layers=layers, neurons=neurons, pulse_layers=pulse_layers, loops=loops )
			#Bjitdriver.jit_merge_point( layers=layers, neurons=neurons, pulse_layers=pulse_layers, loops=loops)
			now = float( time.time() )
			self._fps = loops / float(now-start)
			for i,lay in enumerate(self._layers):
				if self._pulse_layers[i] and False:
					#print 'pulse layer: %s neurons: %s ' %(i, len(lay))
					for n in lay:
						if random()*random() > 0.8:
							n.spike( now )
			for i,col in enumerate(self._columns):
				if self._pulse_columns[i]:
					for n in col: n.spike(now)
			for n in self._neurons:
				n.iterate()
				n.draw(self.screen)
			#r,w,x = rpoll.select( [self._stdin], [], [], 1 )	# wait
			rl,wl,xl = rpoll.select( [0], [], [], 0.000001 )	# wait
			if rl:
				cmd = self._stdin.readline().strip('\n').strip(' ')
				self.do_command( cmd )
			loops += 1
			self._iterations = loops
			#print loops		# can not always print in mainloop, then select can never read from stdin
			RSDL.Flip(self.screen)
			#self._fps = float(time.time()) - now
		#return loops
		return 0