避免多线程UDP服务器中的数据丢失

时间:2017-01-26 08:40:15

标签: python multithreading

我有这个脚本创建一个UDP服务器,它接收一个流并将其放入一个数组中。每分钟后,我将数据摄入,清理并发送到另一台服务器。这两个操作都在共享相同变量的theads上运行。

import socket, time, threading, copy

UDP_IP = "255.255.255.255"
UDP_PORT = 4032

store = []

lock = threading.Lock()

def receive_data():
    global queue
    global lock

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((UDP_IP, UDP_PORT))

    while True:
        data = s.recv(9999)
        # store data temporarily
        lock.acquire()
        store.append(data)
        lock.release()

def send_data():
    global store
    global lock

    lock.acquire()
    data = copy.deepcopy(store)
    store = []
    lock.release()

    # Clean up, send and put a timer
    threading.Timer(60, send_data).start()

t1 = threading.Thread(target=receive_data, name='Server')
t1.start()

t2 = threading.Thread(target=send_data, name='Sender')
t2.start() 

我的问题:在避免数据丢失方面,这是一个足够好的脚本吗?我担心锁定变量可能会使UDP服务器保持不变以访问它,并以某种方式跳过在此期间发送的数据。

1 个答案:

答案 0 :(得分:1)

假设你的代码是这样的:

import socket, time, threading, copy

UDP_IP = "255.255.255.255"
UDP_PORT = 4032

store = []

lock = threading.Lock()

def receive_data():
    global store
    global lock
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((UDP_IP, UDP_PORT))

    while True:
        data = s.recv(9999)
        # store data temporarily
        lock.acquire()  # Note the lock around access to global store
        store.append(data)
        lock.release()

def send_data():
    global store
    global lock

    lock.acquire()
    data = store[:]  # cheap copy of the contents while locked
    store = []
    lock.release()

    # Expensive processing of data to send it to another server
    process(data)

    # Clean up, send and put a timer
    threading.Timer(60, send_data).start()

t1 = threading.Thread(target=receive_data, name='Server')
t1.start()

t2 = threading.Thread(target=send_data, name='Sender')
t2.start() 

然后就阅读数据而言没有任何阻碍。无论如何,套接字将为您缓冲数据。