在后台运行python服务器

2024-04-24 16:54:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用gevent-socketio将实时数据从python脚本推送到浏览器。不过,我希望它能从解释器交互工作,这样服务器就可以继续在后台运行了。一些伪代码:

#start server with the application and keep it running on the background
server_inst=MyAppServer()

#send data to the sever that pushes it to a browser (prints "foo" to the browser)
server_inst.send_data('foo')

看了线程之后,我仍然不明白该怎么做。有什么建议吗。在


Tags: theto数据browser脚本senddataserver
1条回答
网友
1楼 · 发布于 2024-04-24 16:54:46

是否有理由将服务器和控制台程序作为同一进程?在

如果没有,我建议使用两个独立的进程和命名管道。如果这个解决方案是不理想的,无论什么原因,你可以提供更多的细节。在

总之,这里有一些代码可能对实现管道类很有用。我不确定需要通信的数据的确切形式,这样就可以使管道类抽象一些简单的协议和/或使用pickle。在

def __init__(self,sPath):
    """
    create the fifo. if it already exists just associate with it
    """
    self.sPath = sPath
    if not os.path.exists(sPath):
        try:
            os.mkfifo(sPath)
        except:
            raise Exception('cannot mkfifo at path \n {0}'.format(sPath))
    self.iFH = os.open(sPath,os.O_RDWR | os.O_NONBLOCK)
    self.iFHBlocking = os.open(sPath,os.O_RDWR)

def base_read(self,iLen,blocking=False):
    iFile = self.iFHBlocking if blocking else self.iFH
    while not self.ready_for_reading():
        import time
        time.sleep(0.5)

    lBytes = ''.encode('utf-8')
    while len(lBytes)<iLen:
        self.lock()
        try:
            lBytes += os.read(iFile,1)
            self.unlock()
        except OSError as e:
            self.unlock()
            if e.errno == 11:
                import time
                time.sleep(0.5)
            else:
                raise e

    return lBytes

def base_write(self,lBytes,blocking = False):
    iFile = self.iFHBlocking if blocking else self.iFH
    while not self.ready_for_writing():
        import time
        time.sleep(0.5)

    while True:
        self.lock()
        try:
            iBytesWritten =  os.write(iFile, lBytes)
            self.unlock()
            break
        except OSError as e:
            self.unlock()
            if e.errno in [35,11]:
                import time
                time.sleep(0.5)
            else:
                raise

    if iBytesWritten < len(lBytes):
        self.base_write(lBytes[iBytesWritten:],blocking)

def get_lock_dir(self):
    return '{0}_lockdir__'.format(self.sPath)

def lock(self):
    while True:
        try:
            os.mkdir(self.get_lock_dir())
            return
        except OSError as e:
            if e.errno != 17:
                raise e

def unlock(self):
    try:
        os.rmdir(self.get_lock_dir())
    except OSError as e:
        if e.errno != 2:
            raise e

def ready_for_reading(self):
    lR,lW,lX = select.select([self.iFH,],[],[],self.iTimeout)
    if not lR:
        return False
    lR,lW,lX = select.select([self.iFHBlocking],[],[],self.iTimeout)
    if not lR:
        return False
    return True

def ready_for_writing(self):
    lR,lW,lX = select.select([],[self.iFH,],[],self.iTimeout)
    if not lW:
        return False
    return True

我有一个更完整的类实现,我愿意与大家分享,但我认为我抽象的协议可能对你没用。。。使用此方法的方法是使用相同的路径在服务器和控制台应用程序中创建一个实例。然后使用base_read和base_write来完成脏工作。这里所有其他的东西都是为了避免奇怪的比赛条件。在

希望这有帮助。在

相关问题 更多 >