Skip to content

Commit 5750e2d

Browse files
committed
Merge pull request ipython#1608 from minrk/ar_sugar_2.6
don't rely on timedelta.total_seconds in AsyncResult (timedelta is new in python 2.7). This maintains compatibility with Python 2.6; the utility function introduced can be removed if/when we drop 2.6 support.
2 parents 9b9fb9e + 63ca28c commit 5750e2d

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

IPython/parallel/client/asyncresult.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
2525
from IPython.external.decorator import decorator
2626
from IPython.parallel import error
2727

28+
#-----------------------------------------------------------------------------
29+
# Functions
30+
#-----------------------------------------------------------------------------
31+
32+
def _total_seconds(td):
33+
"""timedelta.total_seconds was added in 2.7"""
34+
try:
35+
# Python >= 2.7
36+
return td.total_seconds()
37+
except AttributeError:
38+
# Python 2.6
39+
return 1e-6 * (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6)
2840

2941
#-----------------------------------------------------------------------------
3042
# Classes
@@ -300,7 +312,7 @@ def timedelta(self, start, end, start_key=min, end_key=max):
300312
# handle single_result AsyncResults, where ar.stamp is single object,
301313
# not a list
302314
end = end_key(end)
303-
return (end - start).total_seconds()
315+
return _total_seconds(end - start)
304316

305317
@property
306318
def progress(self):
@@ -323,7 +335,7 @@ def elapsed(self):
323335
stamp = self._client.metadata[msg_id]['submitted']
324336
if stamp and stamp < submitted:
325337
submitted = stamp
326-
return (now-submitted).total_seconds()
338+
return _total_seconds(now-submitted)
327339

328340
@property
329341
@check_ready
@@ -334,7 +346,7 @@ def serial_time(self):
334346
"""
335347
t = 0
336348
for md in self._metadata:
337-
t += (md['completed'] - md['started']).total_seconds()
349+
t += _total_seconds(md['completed'] - md['started'])
338350
return t
339351

340352
@property

0 commit comments

Comments
 (0)