如何管理TCP客户端读/写重叠问题?

时间:2012-01-25 22:21:42

标签: multithreading sockets tcp thread-safety labview

我有一个TCP客户端与LabVIEW GUI通信。

我的程序在开始时调用connect(),在结尾调用disconnect()。它将调用passCommand(x)来读取或写入LabVIEW GUI的数据。但是,在某些情况下,我有多个线程可能正在调用passCommand(),并且返回数据会以某种方式混淆。

例如,在主线程中,我将询问电压,该电压应该是介于300和400之间的数字。在不同的线程中,我将询问温度,该温度应为0-100之间的数字。电压将返回25,而温度将达到250.

这是TCP通信和线程的已知问题吗?有没有办法解决这个问题,例如实现队列或唯一ID还是什么?

import socket as _socket

# get python major version as integer
from sys import version as pythonVersion
pythonVersionMajor = int(pythonVersion[0])

_serverHost = 'localhost'
_serverPort = 50007
isConnected = 0
_sockobj = None
_error_string = "error:"


def connect():
    'opens a connection to LabVIEW Server'
    global _sockobj, isConnected
    _sockobj = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)      # create socket
    _sockobj.connect((_serverHost, _serverPort))   # connect to LV
    isConnected = 1


def disconnect():
    'closes the connection to LabVIEW Server'
    global isConnected
    _sockobj.close()                             # close socket
    isConnected = 0


def passCommand(command):
    'passes a command to LabVIEW Server'

    ## We prepend the command length (8 char long) to the message and send it to LV
    # Compute message length and pad with 0 on the left if required
    commandSize=str(len(command)).rjust(8,'0')
    # Prepend msg size to msg
    completeCommand=commandSize+command
    # python 3 requires data to be encoded
    if (pythonVersionMajor >= 3):
        completeCommand = str.encode(completeCommand)
    # Send complete command
    _sockobj.send(completeCommand)
    data = _sockobj.recv(11565536)
    # python 3 requires data to be decoded
    if (pythonVersionMajor >= 3):
        data = bytes.decode(data)
    if data.rfind(_error_string) == 0:
        error = True
        data = data[len(_error_string):] # get data after "error:" string
    else:
        error = False
    execString = "lvdata = " + data
    exec(execString, globals())
    if error:
        raise _LabVIEWError(lvdata)
    else:
        return lvdata


class _Error(Exception):
    """Base class for exceptions in this module."""
    pass


class _LabVIEWError(_Error):
    """Exception raised for errors generated in LabVIEW.

    Attributes:
        code -- LabVIEW Error Code
        source -- location of the error
        message -- explanation of the error
    """

    def __init__(self, error):
        self.code = error[0]
        self.source = error[1]        
        self.message = error[2]

    def __str__(self):
        return "%s" % (self.message,)

1 个答案:

答案 0 :(得分:4)

这是线程最常见问题之一的示例。您正在从多个线程访问资源,并且该资源不被视为线程安全(如果两个线程同时发送/接收,则线程可能得到错误的响应,甚至两者回复)。

理想情况下,您应该使用互斥锁锁定对passCommand的访问权限,这样它一次只能由一个线程使用,或者每个线程打开一个套接字,或者在单个线程中执行所有套接字操作。