Python 下 sock.recv 产生阻塞的解决办法 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
sbmzhcn
V2EX    Python

Python 下 sock.recv 产生阻塞的解决办法

  •  
  •   sbmzhcn 2015-06-16 18:32:43 +08:00 5779 次点击
    这是一个创建于 3771 天前的主题,其中的信息可能已经有所发展或是发生改变。
    buf = 8192 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(('192.168.1.10', 2000)) sock.sendall(data) data = sock.recv(buf) sock.close() 

    上面是最简单的socket连接程序,网上很多例子也是这样,但是recv这个地方如果服务器突然不响应了会产生阻塞,避免的办法网上大概有以下几个:
    1. socket.settimeout() ,这个无法避免阻塞的,行不通
    2. socket.setblocking(0) 这个必须再加个time.sleep(5)模拟阻塞,此方法兼容性很不友好。

    想请问大家有什么特别好的方法没?比如异步的写法,给个示例也行。

    11 条回复    2015-06-17 10:33:17 +08:00
    est
        1
    est  
       2015-06-16 18:39:09 +08:00 via Android
    tornado, gevent, asyncio
    binux
        2
    binux  
       2015-06-16 18:42:39 +08:00
    select
    fangjinmin
        3
    fangjinmin  
       2015-06-16 18:46:42 +08:00
    用epoll
    lilydjwg
        4
    lilydjwg  
       2015-06-16 19:12:20 +08:00
    @binux @fangjinmin 这两个都无法解决 recv(2) 阻塞的问题除非把套接字设置成非阻塞。

    不知道你的「无法避免阻塞」是什么意思。settimeout 会在你指定的时间之后返回的,并不会一直阻塞下去啊。你一刻也不想阻塞?可 setblocking(False) 之后,你为什么还要 sleep 呢,按之前的逻辑,这样不就「阻塞」了吗?所以,你的需求到底是什么?
    lilydjwg
        5
    lilydjwg  
       2015-06-16 19:12:48 +08:00   3
    binux
        6
    binux  
       2015-06-16 19:15:42 +08:00
    @lilydjwg 套接字 read ready 的泗洪 recv 也会阻塞吗?
    hualuogeng
        7
    hualuogeng  
       2015-06-16 19:19:06 +08:00
    @lilydjwg 多好的XY
    nirocfz
        8
    nirocfz  
       2015-06-16 20:28:12 +08:00
    @binux 有可能,linux 的 man select 有这么一段话

    Under Linux, select() may report a socket file descriptor as "ready for
    reading", while nevertheless a subsequent read blocks. This could for
    example happen when data has arrived but upon examination has wrong
    checksum and is discarded. There may be other circumstances in which a
    file descriptor is spuriously reported as ready. Thus it may be safer
    to use O_NONBLOCK on sockets that should not block.
    way2exluren
        9
    way2exluren  
       2015-06-16 20:53:40 +08:00
    python 的socket在设置timeout后。
    sock.timeout(5)
    socket就变成非阻塞socket了……
    楼主注意
    zeayes
        10
    zeayes  
       2015-06-17 09:00:52 +08:00
    setblocking或者settimeout,然后catch住特定的异常处理掉,就好了。
    feisan
        11
    feisan  
       2015-06-17 10:33:17 +08:00
    @way2exluren

    设置timeout会使socket变非阻塞?
    sock.settimeout(0)才相当于sock.setblocking(0)吧?
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2369 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 15:49 PVG 23:49 LAX 08:49 JFK 11:49
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86