Esempio n. 1
0
def SocketClient(address):
    '''
    Return a connection object connected to the socket given by `address`
    '''
    family = address_type(address)
    with socket.socket( getattr(socket, family) ) as s:
        s.setblocking(True)
        t = _init_timeout()

        while 1:
            try:
                s.connect(address)
            except socket.error as e:
                if e.args[0] != errno.ECONNREFUSED or _check_timeout(t):
                    debug('failed to connect to address %s', address)
                    raise
                time.sleep(0.01)
            else:
                break
        else:
            raise

        fd = duplicate(s.fileno())
    conn = _multiprocessing.Connection(fd)
    return conn
Esempio n. 2
0
 def accept(self):
     s, self._last_accepted = self._socket.accept()
     s.setblocking(True)
     fd = duplicate(s.fileno())
     conn = _multiprocessing.Connection(fd)
     s.close()
     return conn
Esempio n. 3
0
def reduce_handle(handle):
    if Popen.thread_is_spawning():
        return (None, Popen.duplicate_for_child(handle), True)
    dup_handle = duplicate(handle)
    _cache.add(dup_handle)
    sub_debug('reducing handle %d', handle)
    return (_get_listener().address, dup_handle, False)
 def accept(self):
     s, self._last_accepted = self._socket.accept()
     s.setblocking(True)
     fd = duplicate(s.fileno())
     conn = _multiprocessing.Connection(fd)
     s.close()
     return conn
Esempio n. 5
0
def SocketClient(address):
    """
    Return a connection object connected to the socket given by `address`
    """
    family = address_type(address)
    s = socket.socket(getattr(socket, family))
    t = _init_timeout()
    while 1:
        try:
            s.connect(address)
        except socket.error as e:
            if e.args[0] != errno.ECONNREFUSED or _check_timeout(t):
                debug('failed to connect to address %s', address)
                raise
            time.sleep(0.01)
        else:
            break

    else:
        raise

    fd = duplicate(s.fileno())
    conn = _multiprocessing.Connection(fd)
    s.close()
    return conn
Esempio n. 6
0
 def send_handle(conn, handle, destination_pid):
     process_handle = win32.OpenProcess(win32.PROCESS_ALL_ACCESS, False, destination_pid)
     try:
         new_handle = duplicate(handle, process_handle)
         conn.send(new_handle)
     finally:
         close(process_handle)
Esempio n. 7
0
def reduce_handle(handle):
    if Popen.thread_is_spawning():
        return (None, Popen.duplicate_for_child(handle), True)
    dup_handle = duplicate(handle)
    _cache.add(dup_handle)
    sub_debug('reducing handle %d', handle)
    return (_get_listener().address, dup_handle, False)
Esempio n. 8
0
 def send_handle(conn, handle, destination_pid):
     process_handle = win32.OpenProcess(win32.PROCESS_ALL_ACCESS, False, destination_pid)
     try:
         new_handle = duplicate(handle, process_handle)
         conn.send(new_handle)
     finally:
         close(process_handle)
Esempio n. 9
0
 def accept(self):
     s, self._last_accepted = self._socket.accept()
     # non-blocking sockets fix for issue 6056
     s.settimeout(None)
     fd = duplicate(s.fileno())
     conn = _multiprocessing.Connection(fd)
     s.close()
     return conn
Esempio n. 10
0
 def accept(self):
     s, self._last_accepted = self._socket.accept()
     # non-blocking sockets fix for issue 6056
     s.settimeout(None)
     fd = duplicate(s.fileno())
     conn = _multiprocessing.Connection(fd)
     s.close()
     return conn
Esempio n. 11
0
 def accept(self):
     while True:
         try:
             s, self._last_accepted = self._socket.accept()
         except socket.error as e:
             if e.args[0] != errno.EINTR:
                 raise
         else:
             break
     fd = duplicate(s.fileno())
     conn = _multiprocessing.Connection(fd)
     s.close()
     return conn
Esempio n. 12
0
def other_process_run():
    sock_other = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock_other.bind(server_address)
    sock_other.listen(1)
    c, address = sock_other.accept()
    print "address=" + address
    fd = duplicate(c.fileno())
    c.close()
    conn = Connection(fd)
    try:
        print conn.recv_bytes(10)
    finally:
        conn.close()
    print "other process exit"
Esempio n. 13
0
 def accept(self):
     while True:
         try:
             s, self._last_accepted = self._socket.accept()
         except socket.error as e:
             if e.args[0] != errno.EINTR:
                 raise
         else:
             break
     s.setblocking(True)
     fd = duplicate(s.fileno())
     conn = _multiprocessing.Connection(fd)
     s.close()
     return conn
Esempio n. 14
0
def SocketClient(address):
    family = address_type(address)
    s = socket.socket(getattr(socket, family))
    t = _init_timeout()
    while 1:
        try:
            s.connect(address)
        except socket.error as e:
            if e.args[0] != errno.ECONNREFUSED or _check_timeout(t):
                debug('failed to connect to address %s', address)
                raise
            time.sleep(0.01)
        else:
            break

    else:
        raise

    fd = duplicate(s.fileno())
    conn = _multiprocessing.Connection(fd)
    s.close()
    return conn
Esempio n. 15
0
      def __init__(self, process_obj, env):
        # No super init call by intention!

        from multiprocessing.forking import duplicate, get_command_line, _python_exe, close, get_preparation_data, HIGHEST_PROTOCOL, dump
        import msvcrt
        import _subprocess

        # create pipe for communication with child
        rfd, wfd = os.pipe()

        # get handle for read end of the pipe and make it inheritable
        rhandle = duplicate(msvcrt.get_osfhandle(rfd), inheritable=True)
        os.close(rfd)

        # start process
        cmd = get_command_line() + [rhandle]
        cmd = ' '.join('"%s"' % x for x in cmd)
        hp, ht, pid, tid = _subprocess.CreateProcess(
          _python_exe, cmd, None, None, 1, 0, env, None, None
        )
        ht.Close()
        close(rhandle)

        # set attributes of self
        self.pid = pid
        self.returncode = None
        self._handle = hp

        # send information to child
        prep_data = get_preparation_data(process_obj._name)
        to_child = os.fdopen(wfd, 'wb')
        mp_Popen._tls.process_handle = int(hp)
        try:
          dump(prep_data, to_child, HIGHEST_PROTOCOL)
          dump(process_obj, to_child, HIGHEST_PROTOCOL)
        finally:
          del mp_Popen._tls.process_handle
          to_child.close()
Esempio n. 16
0
      def __init__(self, process_obj, env):
        # No super init call by intention!

        from multiprocessing.forking import duplicate, get_command_line, _python_exe, close, get_preparation_data, HIGHEST_PROTOCOL, dump
        import msvcrt
        import _subprocess

        # create pipe for communication with child
        rfd, wfd = os.pipe()

        # get handle for read end of the pipe and make it inheritable
        rhandle = duplicate(msvcrt.get_osfhandle(rfd), inheritable=True)
        os.close(rfd)

        # start process
        cmd = get_command_line() + [rhandle]
        cmd = ' '.join('"%s"' % x for x in cmd)
        hp, ht, pid, tid = _subprocess.CreateProcess(
          _python_exe, cmd, None, None, 1, 0, env, None, None
        )
        ht.Close()
        close(rhandle)

        # set attributes of self
        self.pid = pid
        self.returncode = None
        self._handle = hp

        # send information to child
        prep_data = get_preparation_data(process_obj._name)
        to_child = os.fdopen(wfd, 'wb')
        mp_Popen._tls.process_handle = int(hp)
        try:
          dump(prep_data, to_child, HIGHEST_PROTOCOL)
          dump(process_obj, to_child, HIGHEST_PROTOCOL)
        finally:
          del mp_Popen._tls.process_handle
          to_child.close()
def SocketClient(address):
    '''
    Return a connection object connected to the socket given by `address`
    '''
    family = address_type(address)
    s = socket.socket( getattr(socket, family) )

    while 1:
        try:
            s.connect(address)
        except socket.error as e:
            if e.args[0] != errno.ECONNREFUSED: # connection refused
                debug('failed to connect to address %s', address)
                raise
            time.sleep(0.01)
        else:
            break
    else:
        raise

    fd = duplicate(s.fileno())
    conn = _multiprocessing.Connection(fd)
    s.close()
    return conn
Esempio n. 18
0
def SocketClient(address):
    family = getattr(socket, address_type(address))
    t = _init_timeout()
    while 1:
        s = socket.socket(family)
        s.setblocking(True)
        try:
            s.connect(address)
        except socket.error as e:
            s.close()
            if e.args[0] != errno.ECONNREFUSED or _check_timeout(t):
                debug('failed to connect to address %s', address)
                raise
            time.sleep(0.01)
        else:
            break

    else:
        raise

    fd = duplicate(s.fileno())
    conn = _multiprocessing.Connection(fd)
    s.close()
    return conn
Esempio n. 19
0
    s = socket.socket(getattr(socket, family))

    while 1:
        try:
            s.connect(address)
        except socket.error, e:
            if e.args[0] != errno.ECONNREFUSED:  # connection refused
                debug('failed to connect to address %s', address)
                raise
            time.sleep(0.01)
        else:
            break
    else:
        raise

    fd = duplicate(s.fileno())
    conn = _multiprocessing.Connection(fd)
    s.close()
    return conn


#
# Definitions for connections based on named pipes
#

if sys.platform == 'win32':

    class PipeListener(object):
        '''
        Representation of a named pipe
        '''
Esempio n. 20
0
    t = _init_timeout()

    while 1:
        try:
            s.connect(address)
        except socket.error, e:
            if e.args[0] != errno.ECONNREFUSED or _check_timeout(t):
                debug('failed to connect to address %s', address)
                raise
            time.sleep(0.01)
        else:
            break
    else:
        raise

    fd = duplicate(s.fileno())
    conn = _multiprocessing.Connection(fd)
    s.close()
    return conn

#
# Definitions for connections based on named pipes
#

if sys.platform == 'win32':

    class PipeListener(object):
        '''
        Representation of a named pipe
        '''
        def __init__(self, address, backlog=None):
Esempio n. 21
0
 def copy_fd(fd):
     rhandle = duplicate(msvcrt.get_osfhandle(fd), inheritable=True)
     os.close(fd)
     return rhandle
Esempio n. 22
0
 def copy_socket(fd):
     rhandle = duplicate(fd, inheritable=True)
     return rhandle
Esempio n. 23
0
def other_process_run():
    sock_other = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock_other.bind(server_address)
    sock_other.listen(1)
    c, address = sock_other.accept()
    print "address=" + address
    fd = duplicate(c.fileno())
    c.close()
    conn = Connection(fd)
    try:
        print conn.recv_bytes(10)
    finally:
        conn.close()
    print "other process exit"

p = Process(target=other_process_run)
p.start()

sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sleep(1)
sock.connect(server_address)
fd = duplicate(sock.fileno())
conn = Connection(fd)
sock.close()
conn.send_bytes("hello")
conn.close()

p.join()
print "main process exit"