單程序設定為非阻塞
from socket import
*# 建立乙個物件
tcp_server = socket(af_inet, sock_stream)
# 設定物件為非阻塞
tcp_server.setblocking(
false
)# 繫結位址和埠
tcp_server.bind((""
,6667))
# 設定監聽的數量
tcp_server.listen(3)
socket_lists =
while
true
:# 等待連線
try:
new_socket, address = tcp_server.accept(
)# 設定新socket為非阻塞
new_socket.setblocking(
false
)(new_socket, address)
)except
:pass
else
:print
(address)
for sock, address in socket_lists:
try:
data = sock.recv(
1024
)except
:pass
else
:if data:
print
(f"接收到資訊: "
)else
:# 關閉連線
sock.close(
) socket_lists.remove(
(sock, address)
)
多程序併發伺服器from socket import
*from multiprocessing import process
defrec_data
(new_socket, address)
:while
true
: data = new_socket.recv(
1024
)if data:
print
(f"接收到資訊: "
)else
:# 關閉連線
new_socket.close(
)def
main()
:# 建立乙個物件
tcp_server = socket(af_inet, sock_stream)
# 繫結位址和埠
tcp_server.bind((""
,6667))
# 設定監聽的數量,超過3個就不再接收,此時的tcp_server只能夠接聽
tcp_server.listen(3)
while
true
:# 接收資料,獲取新的socket,new_socket即可以接收也可以傳送
new_socket, address = tcp_server.accept(
)print
(address)
p = process(target=rec_data, args=
(new_socket, address)
) p.start(
) new_socket.close(
)if __name__ ==
'__main__'
: main(
)
多執行緒併發伺服器from socket import
*from threading import thread
defrec_data
(new_socket, address)
:while
true
: data = new_socket.recv(
1024
)if data:
print
(f"接收到資訊: "
)else
:# 關閉連線
new_socket.close(
)def
main()
:# 建立乙個物件
tcp_server = socket(af_inet, sock_stream)
# 繫結位址和埠
tcp_server.bind((""
,6667))
# 設定監聽的數量,超過3個就不再接收,此時的tcp_server只能夠接聽
tcp_server.listen(3)
while
true
:# 接收資料,獲取新的socket,new_socket即可以接收也可以傳送
new_socket, address = tcp_server.accept(
) p = thread(target=rec_data, args=
(new_socket, address)
) p.start(
)print
(address)
# new_socket.close()
if __name__ ==
'__main__'
: main(
)
io多路復用(輪詢方式)import select
from socket import
*def
main()
:# 建立乙個物件
tcp_server = socket(af_inet, sock_stream)
tcp_server.setsockopt(sol_socket, so_reuseaddr,1)
# 繫結位址和埠
tcp_server.bind((""
,6667))
# 設定監聽的數量,超過3個就不再接收,此時的tcp_server只能夠接聽
tcp_server.listen(3)
sock_lists =
[tcp_server]
while
true
: socket_lists, _, _ = select.select(sock_lists,
,)for sock in socket_lists:
if sock == tcp_server:
new_socket, address = tcp_server.accept(
)print
(address)
else
: data = sock.recv(
1024
)if data:
print
(f"接收到"
)else
: sock.close(
) sock_lists.remove(sock)
if __name__ ==
'__main__'
: main(
)
epoll多路復用(事件通知機制)from socket import
*import select
defmain()
:# 建立物件
tcp_server = socket(af_inet, sock_stream)
tcp_server.setsockopt(sol_socket, so_reuseaddr,1)
# 繫結ip和埠
tcp_server.bind((""
,2223))
# 設定監聽
tcp_server.listen(5)
epoll = select.epoll(
) epoll.register(tcp_server.fileno(
), select.epollin)
clients =
while
true
: epoll_list = epoll.poll(
)for fd, events in epoll_list:
print
(fd, events)
if fd == tcp_server.fileno():
new_socket, address = tcp_server.accept(
)print
(address)
epoll.register(new_socket.fileno(
), select.epollin)
# 將新生成的socket物件儲存到字典中
clients[new_socket.fileno()]
= new_socket
else
: data = clients[fd]
.recv(
1024
)if data:
print
(f"接收到"
)else
: clients[fd]
.close(
)# 登出檔案符
epoll.unregister(fd)
# 刪除socket物件
del clients[fd]
if __name__ ==
'__main__'
: main(
)
select和epoll的區別
select: 有連線限制 32位系統 : 1024個, 64位系統:2048個. 採用輪詢機制
epoll: 沒有連線限制,採用事件通知機制
網路程式設計的併發問題
風吹山崗 2016 05 29 1.在socket與serversocket之間收發流有getinputstream 和getoutputstream 兩個類socket serversocket 三個方法 accept getinputstream 和getoutputstream 在socket...
解決併發問題
一.使用redis鎖 智慧型雲 工單系統 搶單 工單id鎖key public static final string cloud live gain order lock orderid cloud live gain order lock orderid component public cla...
Connection併發問題
connection lock getconnection lock.setautocommit false lock.createstatement execute select from zhaojianyong for update final connection connection ge...