python超時問題

2021-10-02 04:00:37 字數 3234 閱讀 1593

在寫程式的時候會在while迴圈中卡死,或者在介面中卡死等問題。這一切都可以通過超時函式來解決,讓你的程式跳出死迴圈。

下面將通過客服端和服務端給出乙個簡單的例子:包含了多執行緒、修飾函式、超時問題socket通訊等。

服務端,一直跑不斷線,等你來連線:

#!usr/bin/python3

# -*- coding:utf-8 -*-

# author:singweek

import socket

import random

import threading

import inspect

import ctypes

def _async_raise(tid, exctype):

tid = ctypes.c_long(tid)

if not inspect.isclass(exctype):

exctype = type(exctype)

res = ctypes.pythonapi.pythreadstate_setasyncexc(tid, ctypes.py_object(exctype))

if res == 0:

raise valueerror("invalid thread id")

elif res != 1:

ctypes.pythonapi.pythreadstate_setasyncexc(tid, none)

raise systemerror("pythreadstate_setasyncexc failed")

def stop_thread(thread):

_async_raise(thread.ident, systemexit)

def mytimeout(ctime):

"""修飾函式之超時函式

:param ctime:超時時間

:return:

"""ctime=ctime

def timeout(func):

def call():

t=threading.thread(target=func)

t.setdaemon(true)

t.start()

t.join(ctime)

# print("超時!")

return call

return timeout

class test():

def __init__(self):

bind_ip = "127.0.0.1"

bind_port = 8080 #這裡繫結的埠必須得和客戶端訪問的埠是一致的

self.server = socket.socket(socket.af_inet, socket.sock_stream)

self.server.bind((bind_ip, bind_port))

self.server.listen(9)

self.client=''

self.addr=''

self.flags=false

"""mytimeout修飾的函式自定義寫的是不能向裡面傳遞引數,所以通過class self來實現引數改變值的傳遞"""

def out(self):

@mytimeout(5)#設定5秒超時

def accept():

self.client=''

self.addr=''

self.client, self.addr = self.server.accept()

accept()

return self.client,self.addr

def handle_client(client_socket,data):

request = client_socket.recv(1024)

print("server received:%s" % request.decode())

tmp="服務端傳送資料:%s"%data

client_socket.send(tmp.encode())

client_socket.close()

recv=test()

data=[1,2,3,4,5,6,7,8,9,10]

flags=true

while true:

if flags==true:

print("----------------------------")

client, addr = recv.out()

if client!='' and addr!='':

# print("[*]accepted connection from:%s:%d" % (addr[0], addr[1]))

client_handler = threading.thread(target = handle_client, args = (client,data[random.randint(0,9)]))

client_handler.start()

else:

print(client, addr)

else:

pass

客戶端,一直跑,不斷線,等你上線:

#!usr/bin/python3

# -*- coding:utf-8 -*-

# author:singweek

target_host = "127.0.0.1"

target_port = 8080

import socket

import time

def wifi_send_data(target_host = "127.0.0.1",target_port = 8080,data=''):

client = socket.socket(socket.af_inet, socket.sock_stream)

client.connect((target_host, target_port))

client.send(data.encode())

response = client.recv(4096).decode()

print(response)

time.sleep(2)

while true:

try:

wifi_send_data(data="測試時間%s"%time.time())

except exception as e:

print(e)

python使用pip安裝超時問題

pip install 需要安裝的包名 i 鏈結位址 trusted host 網域名稱 例如 使用阿里雲映象安裝 pip install pygame i trusted host mirrors.aliyun.com trusted host mirrors.aliyun.com.這是為了獲得s...

web service超時問題

錯誤資訊 資訊 100 continue read timeout.resume sending the request 資訊 discarding unexpected response http 1.1 100 continue 訪問時間要70s左右 客戶端 上加 遠端呼叫銀聯系統 通過 客戶端...

dubbo超時問題

dubbo是阿里開源的分布式遠端呼叫方案 rpc 由於網路或服務端不可靠,會導致呼叫出現一種不確定的中間狀態 超時 為了避免超時導致客戶端資源 執行緒 掛起耗盡,必須設定超時時間。provider可以配置的consumer端主要屬性有timeout retries loadbalance activ...