2014-01-25 Guido van Rossum <[email protected]>:
>> It looks like Charles-François doesn't like it:
>> http://bugs.python.org/issue20311#msg209213
>
> Hm.
>
>> My latest patch is more generic because it uses also the resolution of
>> the clock.
>
> That sounds right.
>
> However, now that I see the code, I have a few questions. (Sorry for
> shooting first and asking questions later.)
>
> - Is it intentional that granularity is a public attribute of the event
> loop? (I can see a use case but also abuses and confusion.)
> - Is it intentional that granularity can be *set* by users of the event
> loop?

Ah, it was one point in my TODO list, I forgot to mention it :)

I see two use cases:

- Override the time() method in an event loop: you must be able to
modify the granularity using the new clock resolution. (Same point for
a custom selector.)
- On an embedded device, you may prefer to limit power compution
instead of providing the best reactivity. For an user interface, a
granularity of 10 ms may be enough, no need to react in 1 nanosecond.

I'm not sure that the second use case is related to what I wrote in
_run_once(). Rounding the deadline (away from zero) should maybe be
done in call_later() instead.

I'm fine with a read-only property using a private _granularity attribute.

> - Personally I would write the code differently -- I would not change the
> timeout passed to the selector at all, and when going over the _scheduled
> list I would not use ceil() but write it like this:
>
>         now = self.time()
>         while self._scheduled:
>             handle = self._scheduled[0]
>             if handle._when > now + self.granularity:
>                 break
>             handle = heapq.heappop(self._scheduled)
>             self._ready.append(handle)

I tested: the unit test pass. I prefer your version because it is
simpler (it doesn't need the math module). See attached patch. Feel
free to commit it, or tell if I can push it.

Victor
diff -r a9270ac18ecc asyncio/base_events.py
--- a/asyncio/base_events.py	Sat Jan 25 22:02:14 2014 +0100
+++ b/asyncio/base_events.py	Sat Jan 25 23:24:01 2014 +0100
@@ -18,7 +18,6 @@ import collections
 import concurrent.futures
 import heapq
 import logging
-import math
 import socket
 import subprocess
 import time
@@ -605,8 +604,6 @@ class BaseEventLoop(events.AbstractEvent
         elif self._scheduled:
             # Compute the desired timeout.
             when = self._scheduled[0]._when
-            # round deadline aways from zero
-            when = math.ceil(when / self.granularity) * self.granularity
             deadline = max(0, when - self.time())
             if timeout is None:
                 timeout = deadline
@@ -632,9 +629,7 @@ class BaseEventLoop(events.AbstractEvent
         self._process_events(event_list)
 
         # Handle 'later' callbacks that are ready.
-        now = self.time()
-        # round current time aways from zero
-        now = math.ceil(now / self.granularity) * self.granularity
+        now = self.time() + self.granularity
         while self._scheduled:
             handle = self._scheduled[0]
             if handle._when > now:

Reply via email to