希望你們都做得很好。我是 Python 新手,正在從事一個小型的客戶端-服務器專案,我從客戶端接收資料,目標是服務器以圖形形式實時繪制這些資料。這是來自服務器部分的代碼,我現在正在努力。
import socket
import sys
import math
import numpy as np
import struct
import time
import os
import ctypes as c
import multiprocessing
import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib import style
HOST = '127.0.0.1'
PORT = 6543
receive_size = 4096
def run_server(shared_data_time, shared_data_signal):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
server.bind((HOST, PORT))
server.listen()
conn, addr = server.accept()
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(receive_size)
if len(data) == 4096:
payload = np.frombuffer(data, dtype = 'float64')
print(payload)
print('received data')
deinterleaved = [payload[idx::2] for idx in range(2)]
shared_data_time = deinterleaved[0]
shared_data_signal = deinterleaved[1]
print(f'received {len(data)} bytes')
if __name__ == '__main__':
HOST = '127.0.0.1'
PORT = 6543
receive_size = 4096
shared_data_time = multiprocessing.Array('f', 2048)
shared_data_signal = multiprocessing.Array('f', 2048)
process1 = multiprocessing.Process(target = run_server, args =(shared_data_time, shared_data_signal))
process1.start()
def animate(i, shared_data_time, shared_data_signal):
ax1.clear()
ax1.plot(shared_data_time, shared_data_signal)
style.use('fivethirtyeight')
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ani = animation.FuncAnimation(fig, animate, fargs = (shared_data_time, shared_data_signal), interval = 100)
plt.show()
服務器和客戶端之間的通信有效,但我只得到空圖,沒有實作。大家可以幫幫我嗎?我真的很感激。
謝謝
uj5u.com熱心網友回復:
無法訪問您連接的服務器,很難確定確切的問題,但請參閱我制作的這個示例,用于通過 sharedmultiprocessing.Array對來自子行程的資料進行影片處理:
import multiprocessing as mp
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
from time import sleep, time
def server(arr_x, arr_y):
#tie shared buffers to numpy arrays
x = np.frombuffer(arr_x.get_obj(), dtype='f4')
y = np.frombuffer(arr_y.get_obj(), dtype='f4')
T = time()
while True:
t = time() - T #elapsed time
#we should technically lock access while we're writing to the array,
# but mistakes in the data are not real important if it's just a gui.
x[:] = np.linspace(t, t np.pi*2, len(x)) #update data in shared array
y[:] = np.sin(x)
sleep(1/30) #data updating faster or slower than animation framerate is not a big issue...
if __name__ == "__main__":
fig = plt.figure()
ax = plt.subplot()
#init data
arr_x = mp.Array('f', 1000) #type "d" == np.dtype("f8")
arr_y = mp.Array('f', 1000)
#tie shared buffers to numpy arrays
x = np.frombuffer(arr_x.get_obj(), dtype='f4')
y = np.frombuffer(arr_y.get_obj(), dtype='f4')
#calculate initial value
x[:] = np.linspace(0, np.pi*2, len(x))
y[:] = np.sin(x)
#daemon process to update values (server)
mp.Process(target=server, args=(arr_x, arr_y), daemon=True).start()
#plot initial data because we need a line instance to update continually
line = ax.plot(x, y)[0]
#fps counting vars
last_second = time()
last_frame = 0
def animate(frame, x, y):
global last_second, last_frame
#might be cleaner to wrap animate and use nonlocal rather than global,
# but it would be more complicated for just this simple example.
#set data with most recent values
line.set_data(x, y)
line.axes.set_xlim(x[0], x[999])
#periodically report fps
interval = time() - last_second
if interval >= 1:
print("fps: ", (frame-last_frame) / interval)
last_second = time()
last_frame = frame
ani = FuncAnimation(fig, animate, fargs=(x, y), interval = 20)
plt.show()
FPS 計數顯然可以很容易地洗掉,并且將共享陣列轉換為 numpy 陣列并不是絕對必要的,但我發現它更容易使用,而且并不困難。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/316994.html
