release candidate rules and timeit API question
OK, now that I have Internet again I can try to get some Python work done and commit what work I was able to do while I was disconnected. But I noticed that we are now working on a release candidate. Are the commit rules any different than for a beta? I specifically want to commit a patch to make using _strptime for time.strptime permanent by ditching the #ifdef code and flesh out the docs. My timeit API question has to do with timeit.default_timer . The docs don't mention it but I think it might be nice to expose it. The specific I would like to have it available for guaranteed use is that both 'threading' and Queue have code that sleep on an ever-increasing interval. They both use time.time for their time measurements which is fine on UNIX but sucks on Windows when you consider the max time both chunks of code wait is .05 secs which is below the accuracy threshold for Windows (according to Tim's intro in the Cookbook; thank god for books when tech goes kapoot). I would like to edit the code so that it uses what timeit.default_timer is set to. Anyone (especially Guido since he is timeit's original author) have a problem with documenting timeit.default_timer? -Brett
Brett> But I noticed that we are now working on a release candidate. Brett> Are the commit rules any different than for a beta? Essentially, bug fixes only unless approved by Guido I think. Skip
[Brett]
... My timeit API question has to do with timeit.default_timer . The docs don't mention it but I think it might be nice to expose it. The specific I would like to have it available for guaranteed use is that both 'threading' and Queue have code that sleep on an ever-increasing interval. They both use time.time for their time measurements which is fine on UNIX but sucks on Windows when you consider the max time both chunks of code wait is .05 secs which is below the accuracy threshold for Windows (according to Tim's intro in the Cookbook; thank god for books when tech goes kapoot). I would like to edit the code so that it uses what timeit.default_timer is set to. Anyone (especially Guido since he is timeit's original author) have a problem with documenting timeit.default_timer?
The sleep loop in threading.py is fine as-is: time.time != time.sleep, and there's no problem on Windows sleeping for small bits of time. The coarse granularity of time.time() on Windows only comes into play if the total timeout specified is < about 0.055 seconds, but that's a very small total timeout value (more typical is a total timeout of a full second or more). Queue uses the same polling loop code, and it's also fine there. It's not so fine that this delicate code is duplicated, so I'd rather see an internal refactoring to use a common backoff-polling class.
Tim> It's not so fine that this delicate code is duplicated, so I'd Tim> rather see an internal refactoring to use a common backoff-polling Tim> class. I recently copied it to my own code as well. I'd like to see it whacked into something reusable. This seems to work: import time class Timeout(Exception): pass def await_condition(predicate, timeout): delay = 0.0005 endtime = time.time() + timeout while True: if predicate(): return remaining = endtime - time.time() if remaining <= 0: # time's up, predicate always failed raise Timeout delay = min(delay * 2, remaining, .05) time.sleep(delay) # reduce CPU usage by using a sleep Skip
Tim> It's not so fine that this delicate code is duplicated, so I'd Tim> rather see an internal refactoring to use a common backoff-polling Tim> class.
I recently copied it to my own code as well. I'd like to see it whacked into something reusable. This seems to work:
import time class Timeout(Exception): pass
def await_condition(predicate, timeout): delay = 0.0005 endtime = time.time() + timeout while True: if predicate(): return remaining = endtime - time.time() if remaining <= 0: # time's up, predicate always failed raise Timeout delay = min(delay * 2, remaining, .05) time.sleep(delay) # reduce CPU usage by using a sleep
Skip
I wonder if the right refactoring wouldn't be to add an acquire with timeout method to the built-in lock type? --Guido van Rossum (home page: http://www.python.org/~guido/)
Skip> import time Skip> class Timeout(Exception): pass Skip> Skip> def await_condition(predicate, timeout): Skip> delay = 0.0005 Skip> endtime = time.time() + timeout Skip> while True: Skip> if predicate(): Skip> return Skip> remaining = endtime - time.time() Skip> if remaining <= 0: # time's up, predicate always failed Skip> raise Timeout Skip> delay = min(delay * 2, remaining, .05) Skip> time.sleep(delay) # reduce CPU usage by using a sleep Skip> Guido> I wonder if the right refactoring wouldn't be to add an acquire Guido> with timeout method to the built-in lock type? In my case I use await_condition() to gracefully empty a Queue of database connection objects at termination time. def check_conn_pool(self): try: conn = self.conn_pool.get(block=0) except Queue.Empty: pass else: conn.close() self._leftovers.append(conn) return len(self._leftovers) == self.poolsize ... def empty_conn_pool(self, poolname, maxwait): self._leftovers = [] try: await_condition(self.check_conn_pool, maxwait) except Timeout: print "could only find", len(self._leftovers), print "connections for", poolname It's a bit clunky, but I wouldn't be able to use an acquire() with a timeout directly. I'd need a Queue.get with a timeout as well. Besides, wouldn't there be places where this progressive backoff would be useful in non-threaded contexts? Skip
Guido> I wonder if the right refactoring wouldn't be to add an acquire Guido> with timeout method to the built-in lock type?
[Sjoerd]
In my case I use await_condition() to gracefully empty a Queue of database connection objects at termination time. [...] It's a bit clunky, but I wouldn't be able to use an acquire() with a timeout directly. I'd need a Queue.get with a timeout as well.
Yes, but that API change would be useful anyway.
Besides, wouldn't there be places where this progressive backoff would be useful in non-threaded contexts?
Unclear, but it's unclear to me if it really is more readable with a helper function to express the condition passed in rather than just written out explicitly (everyone writes these a little different, and that's just fine with me). --Guido van Rossum (home page: http://www.python.org/~guido/)
[Brett]
... My timeit API question has to do with timeit.default_timer . The docs don't mention it but I think it might be nice to expose it. The specific I would like to have it available for guaranteed use is that both 'threading' and Queue have code that sleep on an ever-increasing interval. They both use time.time for their time measurements which is fine on UNIX but sucks on Windows when you consider the max time both chunks of code wait is .05 secs which is below the accuracy threshold for Windows (according to Tim's intro in the Cookbook; thank god for books when tech goes kapoot). I would like to edit the code so that it uses what timeit.default_timer is set to. Anyone (especially Guido since he is timeit's original author) have a problem with documenting timeit.default_timer?
[Tim]
The sleep loop in threading.py is fine as-is: time.time != time.sleep, and there's no problem on Windows sleeping for small bits of time. The coarse granularity of time.time() on Windows only comes into play if the total timeout specified is < about 0.055 seconds, but that's a very small total timeout value (more typical is a total timeout of a full second or more). Queue uses the same polling loop code, and it's also fine there. It's not so fine that this delicate code is duplicated, so I'd rather see an internal refactoring to use a common backoff-polling class.
Agreed, and I'd rather not expose this from the timeit module (which would be a rather strange place to import this timer from). --Guido van Rossum (home page: http://www.python.org/~guido/)
From: "Brett C." <bac@OCF.Berkeley.EDU> Hey, check out the new, cool email address ;-)
But I noticed that we are now working on a release candidate. Are the commit rules any different than for a beta? I specifically want to commit a patch to make using _strptime for time.strptime permanent by ditching the #ifdef code and flesh out the docs.
Docs and bugfixes, yes. Anything else, ask GvR if you think it is essential. Raymond Hettinger
participants (5)
-
Brett C.
-
Guido van Rossum
-
Raymond Hettinger
-
Skip Montanaro
-
Tim Peters