benjaminp / six

Python 2 and 3 compatibility library
https://six.readthedocs.io/
MIT License
980 stars 274 forks source link

Add time.monotonic? #69

Open benjaminp opened 10 years ago

benjaminp commented 10 years ago

Originally reported by: Joshua Harlow (Bitbucket: harlowja, GitHub: harlowja)


A nice to have would be a compat, function that would add-in time.monotonic support for older versions of python (it appeared in 3.3 from what I remember).

Perhaps something like what billiard has?

https://github.com/celery/billiard/blob/master/billiard/five.py#L32


benjaminp commented 8 years ago

Original comment by Joshua Harlow (Bitbucket: harlowja, GitHub: harlowja):


That's fine with me (closing it); openstack went with the monotonic package route.

benjaminp commented 8 years ago

Original comment by Nick T (Bitbucket: nick_timkovich, GitHub: Unknown):


If you need it backported to <3.3, pip install monotonic, import monotonic, monotonic.monotonic() is a cleaner/better solution. >=3.3 it's just a shim around time.monotonic, but for <3.3 it has all the code for all platforms (as opposed to this which is very platform-dependent).

Close this.

benjaminp commented 8 years ago

Original comment by Matěj Cepl (Bitbucket: mcepl, GitHub: mcepl):


I really believe that relative simplicity of six (and thus possibility of simply embedding one file to my project) is value which should be preserved. When we try to reimplement missing parts of Python 3 a little bit weird and not commonplace functions here, it will get rather complicated fast.

I would suggest for @harlowja to just create a separate library which would do the thing and file it on PyPI.

benjaminp commented 10 years ago

Original comment by Joshua Harlow (Bitbucket: harlowja, GitHub: harlowja):


Fair enough, it is a piece of CFFI code (or ctypes code), just would be a nice addition to smooth over the python 2 -> python 3 transition (and from what I can tell certain other libraries like five @ https://github.com/celery/py-amqp/blob/master/amqp/five.py have included it, for better or worse...)

benjaminp commented 10 years ago

I have to say I'm reluctant to start maintaining this complex FFI code, especially since it's technically not a Python 2/3 compatibility concern.

benjaminp commented 10 years ago

Original comment by Joshua Harlow (Bitbucket: harlowja, GitHub: harlowja):


An idea that would work (not using the ctypes approach that billard uses):

#!python

import os

import cffi

ffi = cffi.FFI()
ffi.cdef("""
typedef struct
{
  long int tv_sec;
  long int tv_nsec;
} timespec;

long clock_gettime(long which_clock, timespec *tp);
""")

# From linux/time.h
CLOCK_MONOTONIC = 1
C = ffi.dlopen('librt.so')

def _monotonic():
    t = ffi.new("timespec *")
    e = C.clock_gettime(CLOCK_MONOTONIC, t)
    if e != 0:
        raise OSError(ffi.errno, os.strerror(ffi.errno))
    else:
        return t.tv_sec + (t.tv_nsec * 1e-9)

print _monotonic()

Any thoughts on accepting something like this?