在 Python 中重试连接前暂停 [英] Pause before retry connection in Python

查看:27
本文介绍了在 Python 中重试连接前暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到服务器.有时我无法访问服务器,想在重试之前暂停几秒钟.我将如何在 Python 中实现暂停功能.这是我到目前为止所拥有的.谢谢.

I am trying to connect to a server. Sometimes I cannot reach the server and would like to pause for a few seconds before trying again. How would I implement the pause feature in Python. Here is what I have so far. Thank you.

   while True:
        try:
            response = urllib.request.urlopen(http)
        except URLError as e:
            continue
        break

我使用的是 Python 3.2

I am using Python 3.2

推荐答案

这会阻塞线程 2 秒然后继续:

This will block the thread for 2 seconds before continuing:

import time
time.sleep(2)

这篇关于在 Python 中重试连接前暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆