推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
misaka19000
V2EX  ›  Python

请问 Python 大神, Python 跨平台的代码应该怎么写比较优雅?

  •  
  •   misaka19000 ·
    RitterHou · Jun 4, 2018 · 3337 views
    This topic created in 2900 days ago, the information mentioned may be changed or developed.

    我在进行网络编程的时候需要使用到 select 模型,同时如果平台是 Linux 就使用 epoll 模型,所以我需要对 Linux 平台的代码做特殊处理,目前我的代码是这样的:

    if is_linux():
        self.__fds = {}
        self.__epoll = select.epoll()
    
    if is_linux():
        conn = self._connection_pool[host]
        self.__epoll.register(conn.fileno(), select.EPOLLIN)
        self.__fds[conn.fileno()] = conn
    
    if is_linux():
        while 1:
            events = self.__epoll.poll(1)
    else:
        while 1:
            readable, writeable, exceptional = select.select(conns, [], [])
    
    if conn.close():  # 连接已关闭
        if is_linux():
            self.__epoll.unregister(conn.fileno())
    

    我是对所有需要用到 epoll 的地方添加了环境判断,如果是 linux 环境就做特殊的处理。 以上代码是可以正常使用的,但是感觉不够优雅,不知道各位有没有什么比较优雅的解决方案🤔

    17 replies    2018-06-12 10:46:11 +08:00
    kinghui
        1
    kinghui  
       Jun 4, 2018
    BBCCBB
        2
    BBCCBB  
       Jun 4, 2018   ❤️ 1
    import selectors
    lihongjie0209
        3
    lihongjie0209  
       Jun 4, 2018
    一个接口多个实现类
    misaka19000
        4
    misaka19000  
    OP
       Jun 4, 2018
    @BBCCBB #2 请问能不能再具体一点?
    misaka19000
        5
    misaka19000  
    OP
       Jun 4, 2018
    @lihongjie0209 #3 这个我倒是想过,但是因为大部分代码其实重复的,所以如果分为多个类将会导致这些类中会与比较多的重复代码,感觉还是不够好
    chroming
        6
    chroming  
       Jun 4, 2018   ❤️ 1
    把所有 is_linux 的代码封装为一个类的不同方法

    class SysSelect(object):
    def __init__():
    self.is_linux = is_linux()

    def on_start():
    if self.is_linux():
    pass
    lihongjie0209
        7
    lihongjie0209  
       Jun 4, 2018
    @misaka19000 #5 重复代码放到一个超类中
    raptor
        8
    raptor  
       Jun 4, 2018
    有现成的包装为什么要自己写底层操作?
    BBCCBB
        9
    BBCCBB  
       Jun 4, 2018
    python3.4 之后有一个模块叫 selectors, 就是适配了多个平台的 select 模型, 同一套 api 操作 select, epoll, kqueue 等, 而且自动选择最优的模型
    misaka19000
        10
    misaka19000  
    OP
       Jun 4, 2018
    @raptor #8 请问有什么包呢?
    misaka19000
        11
    misaka19000  
    OP
       Jun 4, 2018
    @BBCCBB #9 在 python2 中是不是没有办法使用?
    BBCCBB
        12
    BBCCBB  
       Jun 4, 2018
    @misaka19000 应该在 python2 中没法用, 我不熟悉 python2 呢
    edsion996
        13
    edsion996  
       Jun 4, 2018
    把 Linux 部分需要特殊处理的代码,通过装饰器来实现??
    lolizeppelin
        14
    lolizeppelin  
       Jun 4, 2018 via Android
    参考 eventlet poll 部分代码即可
    raptor
        15
    raptor  
       Jun 8, 2018
    @misaka19000 我的意思是封装了 epoll 的网络操作包有很多,不知道你有什么不一样的网络操作需要做这样底层的开发,如果真有这种需要,我个人认为并不适合用 python 来做
    misaka19000
        16
    misaka19000  
    OP
       Jun 8, 2018
    @raptor 我是需要开发一个 Python 的 RPC 框架,不知道有没有什么比较合适的库推荐呢?
    lolizeppelin
        17
    lolizeppelin  
       Jun 12, 2018 via Android
    照抄 openstack 的 oslo message
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3659 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 04:40 · PVG 12:40 · LAX 21:40 · JFK 00:40
    ♥ Do have faith in what you're doing.