Skip to content

Commit 320aaa8

Browse files
committed
Use monotonic clock for time measuring
1 parent 747429e commit 320aaa8

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

rosbridge_library/src/rosbridge_library/capabilities/defragmentation.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import threading
2-
from datetime import datetime
2+
import time
33

44
from rosbridge_library.capability import Capability
55

@@ -84,13 +84,13 @@ def __init__(self, protocol):
8484
# 4.b) pass the reconstructed message string to protocol.incoming() # protocol.incoming is checking message fields by itself, so no need to do this before passing the reconstructed message to protocol
8585
# 4.c) remove the fragment list to free up memory
8686
def defragment(self, message):
87-
now = datetime.now()
87+
now = time.monotonic()
8888

8989
if self.received_fragments is not None:
9090
for id in self.received_fragments.keys():
9191
time_diff = now - self.received_fragments[id]["timestamp_last_append"]
9292
if (
93-
time_diff.total_seconds() > self.fragment_timeout
93+
time_diff > self.fragment_timeout
9494
and not self.received_fragments[id]["is_reconstructing"]
9595
):
9696
log_msg = ["fragment list ", str(id), " timed out.."]
@@ -188,15 +188,15 @@ def defragment(self, message):
188188
log_msg = "".join(log_msg)
189189
self.protocol.log("debug", log_msg)
190190

191-
duration = datetime.now() - now
191+
duration = time.monotonic() - now
192192

193193
# Pass the reconstructed message to rosbridge
194194
self.protocol.incoming(reconstructed_msg)
195195
log_msg = ["reconstructed message (ID:" + str(msg_id) + ") from "]
196196
log_msg.extend([str(msg_total), " fragments. "])
197197
# cannot access msg.data if message is a service_response or else!
198198
# log_msg += "[message length: " + str(len(str(json.loads(reconstructed_msg)["msg"]["data"]))) +"]"
199-
log_msg.extend(["[duration: ", str(duration.total_seconds()), " s]"])
199+
log_msg.extend(["[duration: ", str(duration), " s]"])
200200
log_msg = "".join(log_msg)
201201
self.protocol.log("info", log_msg)
202202

rosbridge_library/src/rosbridge_library/internal/subscription_modifiers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
# POSSIBILITY OF SUCH DAMAGE.
3232

3333
import sys
34+
import time
3435
import traceback
3536
from collections import deque
3637
from threading import Condition, Thread
37-
from time import time
3838

3939
""" Sits between incoming messages from a subscription, and the outgoing
4040
publish method. Provides throttling / buffering capabilities.
@@ -66,10 +66,10 @@ def set_queue_length(self, queue_length):
6666
return self.transition()
6767

6868
def time_remaining(self):
69-
return max((self.last_publish + self.throttle_rate) - time(), 0)
69+
return max((self.last_publish + self.throttle_rate) - time.monotonic(), 0)
7070

7171
def handle_message(self, msg):
72-
self.last_publish = time()
72+
self.last_publish = time.monotonic()
7373
self.publish(msg)
7474

7575
def transition(self):

rosbridge_library/test/internal/subscribers/test_subscription_modifiers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ def cb(msg):
146146
handler.publish = cb
147147

148148
self.assertTrue(handler.time_remaining() == 0)
149-
t1 = time.time()
149+
t1 = time.monotonic()
150150
handler.handle_message(msg)
151-
t2 = time.time()
151+
t2 = time.monotonic()
152152

153153
self.assertEqual(received["msg"], msg)
154154
self.assertLessEqual(t1, handler.last_publish)

0 commit comments

Comments
 (0)