当前位置: 首页>>代码示例>>Python>>正文


Python Listener.fileno方法代码示例

本文整理汇总了Python中multiprocessing.connection.Listener.fileno方法的典型用法代码示例。如果您正苦于以下问题:Python Listener.fileno方法的具体用法?Python Listener.fileno怎么用?Python Listener.fileno使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在multiprocessing.connection.Listener的用法示例。


在下文中一共展示了Listener.fileno方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run

# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import fileno [as 别名]
    def run(self):
        """
        Main angel loop
        """
        # Open angel socket
        authkey = self.settings.angel_authkey.encode('utf-8')
        listener = Listener(
            str(self.settings.get_path('angel_socket')),
            self.settings.angel_family,
            authkey=authkey,
        )
        multiprocessing.current_process().authkey = authkey
        
        # Give the listener a fileno so we can use it directly with select
        listener.fileno = listener._listener._socket.fileno
        
        # Main angel loop
        socket_to_process = {}
        process_to_socket = {}
        old_process = None
        start_delay = 0
        self.running = True
        while self.running:
            # We don't have an active process, so there shouldn't be others
            # If there are, terminate them
            for process, process_socket in list(process_to_socket.items()):
                if process.poll():
                    process.terminate()
                process_socket.close()
                del process_to_socket[process]
                del socket_to_process[process_socket]
            
            # There is nothing valid in the store
            # Either this is the first time it's running,
            # or it failed, so didn't get a chance to serialise anything
            store = None
            
            # Start script, with increasing delay if it last start failed
            if start_delay:
                time.sleep(start_delay)
                if start_delay < START_DELAY_MAX:
                    start_delay *= 2
            else:
                start_delay = 1
            self.active_process = self.start_process()
            
            # Loop while the script is running
            while self.active_process.poll() is None:
                # Check old processes for dead
                for process in process_to_socket.keys():
                    if process == self.active_process:
                        # Already checked active process
                        continue
                    if process.poll() is not None:
                        self.log.angel('Process %s has died' % process.pid)
                        process_to_socket[process].close()
                        del process_to_socket[process]
                        del socket_to_process[process_socket]
                
                # Check sockets for activity
                read_sockets = []
                send_sockets = []
                try:
                    read_sockets, send_sockets = select.select(
                        [listener] + list(socket_to_process.keys()), [], [], 1,
                    )[0:2]
                except select.error as e:
                    self.log.angel('Angel select error: %s' % e)
                except socket.error as e:
                    self.log.angel('Angel socket error: %s' % e)
                
                # Check listener socket for new connections
                if listener in read_sockets:
                    # Only leave process sockets on read_sockets
                    del read_sockets[read_sockets.index(listener)]
                    
                    # Accept the connection
                    process_socket = listener.accept()
                    
                    # Check this makes sense
                    if self.active_process in process_to_socket:
                        # We already have a socket for the active process.
                        # This shouldn't happen - only explanation is this is
                        # an intruder. Ignore it.
                        self.log.angel(
                            'New connection for existing process %s ignored' %
                            self.active_process.pid
                        )
                        process_socket.close()
                    
                    # Store against the current process
                    self.log.angel(
                        'Established connection to process %s' %
                        self.active_process.pid
                    )
                    socket_to_process[process_socket] = self.active_process
                    process_to_socket[self.active_process] = process_socket
                    
                # Check process sockets who want to say something
                for process_socket in read_sockets:
#.........这里部分代码省略.........
开发者ID:radiac,项目名称:mara,代码行数:103,代码来源:angel.py


注:本文中的multiprocessing.connection.Listener.fileno方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。