通过Threading.Timer(700ms)将数据发送到服务器后,我在另一侧685ms收到?
我一直在开发一个应用程序,用于测量两个帧之间的差距,以测量摄像机秒表上的时间,因此我需要在特定时间后触发功能才能将数据发送到服务器(Server-TCP)。但是我有一个问题有时服务器接收请求少于指定的时间,例如,如果我以700ms的限制运行线程,我会在服务器内找到一些请求,例如0.682,0.690,0.692,0.699,即使我计算了差距在实际图像中,它也一样。
我期望的正常行为是,如果功能在700ms之后触发,并且我进行了一些过程,则必须大于700毫秒,这是正常行为。
我不知道我犯了什么错误,以及如何发生这种情况,所有请求都有一个唯一的名称,因此很容易追踪它们。
这是我触发将请求发送到服务器的主要核心。
#2nd image
def generate2ndTicketBasedonTime(trackedObject, attributes, object_id,event_obj):
try:
while not event_obj.wait(0.700):
ImageProcessing.getInstance().serviceSnapshot(camera, DataToQeue)
event_obj.set()
pass
#1st image
def snapshot
ImageProcessing.getInstance().serviceSnapshot(camera, DataToQeue)
event_obj = threading.Event()
threading.Thread(target=generate2ndTicketBasedonTime, args=[self, self.attributes, self.car.object_id,event_obj]).start()
# client-side -< send data to the server
def sendRequestToCameraService(self, data):
Request = {'cameraRequest': json.dumps(data)}
self.SendToServer(Request)
def SendToServer(self, data):
try:
if(self.isConnected):
data = json.dumps(data).encode()
dataSize = struct.pack("<i", len(data))
self.currentSocket.sendall(b'\xAA\xBB'+dataSize+data+b'\xCC\xDD')
self.triggerTime = time.time()
except BrokenPipeError:
self.isConnected = False
#server setting
def run(self):
srv_address = "0.0.0.0"
srv_port = 4504
srv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv_sock.bind((srv_address, int(srv_port)))
srv_sock.listen(5)
while True:
cl_sock, cl_address = srv_sock.accept()
ClientHandler(cl_sock, cl_address, f"Client -{len(self.clients)}")
示例
我面临
的 a href =“ https://i.sstatic.net/0y8dz.png” rel =“ nofollow noreferrer”>图像差距之前触发时钟中的第二种方法
I have been developing an application for measuring the gap between two frames to measure the time on the stopwatch by the camera, so I need to trigger a function after a specific time to send data to the server (Server-TCP). but I have one issue sometimes the server receive request less than the specified time, for example, if I run a thread with Timer 700ms, I find some requests inside the server such as 0.682,0.690,0.692,0.699 even if I calculate the gap in the real-image it also the same.
the normal behavior I expected to be if the function Trigger after 700ms, and I do some process, It must be bigger than 700 ms, this is the normal behavior.
I don't know where the mistake I made, and how that was possible to happen, all requests have a unique name, so it's easy to trace them.
this the main core in which I trigger the method to send requests to a server.
#2nd image
def generate2ndTicketBasedonTime(trackedObject, attributes, object_id,event_obj):
try:
while not event_obj.wait(0.700):
ImageProcessing.getInstance().serviceSnapshot(camera, DataToQeue)
event_obj.set()
pass
#1st image
def snapshot
ImageProcessing.getInstance().serviceSnapshot(camera, DataToQeue)
event_obj = threading.Event()
threading.Thread(target=generate2ndTicketBasedonTime, args=[self, self.attributes, self.car.object_id,event_obj]).start()
# client-side -< send data to the server
def sendRequestToCameraService(self, data):
Request = {'cameraRequest': json.dumps(data)}
self.SendToServer(Request)
def SendToServer(self, data):
try:
if(self.isConnected):
data = json.dumps(data).encode()
dataSize = struct.pack("<i", len(data))
self.currentSocket.sendall(b'\xAA\xBB'+dataSize+data+b'\xCC\xDD')
self.triggerTime = time.time()
except BrokenPipeError:
self.isConnected = False
#server setting
def run(self):
srv_address = "0.0.0.0"
srv_port = 4504
srv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv_sock.bind((srv_address, int(srv_port)))
srv_sock.listen(5)
while True:
cl_sock, cl_address = srv_sock.accept()
ClientHandler(cl_sock, cl_address, f"Client -{len(self.clients)}")
example for the case which I'm facing
image for gap time between client and server-side
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我弄清楚了这个问题,正是相机出于神秘的原因在特定时间之前返回框架。
1-我尝试了没有套接字和线程的代码,以避免复杂性,因此主要问题是来自相机
2-启动应用程序之间的差异很小(thing time.time.time()),所以我现在发送从请求中,从主要应用程序到其他服务器的时间戳,因此该过程的序列现在看起来不错。
I figure out the issue, it was the camera that return the frame before the specific time for a mysterious reason.
1- I tried the code without socket and threads, to avoid complexity so the main problem was from the camera
2- there is a slight difference between the applications when they start up(take time.time()) so now I'm sending the timestamp from the main application to other servers in the request, so the sequences for the process seem good now.