Tuesday, January 24, 2012

A simple event loop with PEP 380

On January 13, Nick Coghlan pushed up the C implementation of PEP 380, which officially supports yield from syntax, to Python 3. I have toyed around with yield from idea since P. J. Eby cooked up his fiendishly clever trampoline in pure Python. The official C implementation is a welcomed news because it alleviates the need for such a trampoline from app to app.

Here's a simple event loop that makes use of PEP 380.

import heapq
import inspect
import time

_processes = []

def sleep(delay):
    yield 'sleep', delay

def task(name, delay):
    for i in range(10):
        print('hello {0} {1} {2}'.format(time.time(), name, i))
        yield from sleep(delay)

def spawn(proc, *args, **kw_args):
    def generatorize(f, *args, **kw_args):
        yield 'ignore', f(*args, **kw_args)
    global _processes
    if not inspect.isgenerator(proc):
        if inspect.isgeneratorfunction(proc):
            proc = proc(*args, **kw_args)
            proc = generatorize(proc, *args, **kw_args)
    heapq.heappush(_processes, (time.time(), proc))

def run():
    global _processes
    while _processes:
        (start_time, proc) = heapq.heappop(_processes)
        if start_time > time.time():
            heapq.heappush(_processes, (start_time, proc))
            command, arg = next(proc)
        except StopIteration:
            if command == 'sleep':
                heapq.heappush(_processes, (time.time() + arg, proc))

def not_a_task():
    print('not a task')

spawn(task, 'task 0.5', 0.5)
spawn(task, 'task 1.0', 1.0)


The main functions are spawn and run. In spawn, a function object is converted into a generator if needed and then pushed into a heap queue of scheduled processes. These processes are given a chance to run when they are up in schedule. They can voluntarily yield execution to other processes by calling yield from sleep, or some other utility functions such as read or write.

Overall, I think the code is quite compact and readable.

Friday, January 20, 2012

How to build Python on Mac OS X Lion

Apparently this question comes up from time to time on python-dev@ (which, by the way, isn't the right list for these sorts of question).

So here's the short answer:

./configure CC=clang

There, enjoy!

For more information, one should refer to this bug report: http://bugs.python.org/issue13241.

Tuesday, January 10, 2012

Google+ integrated into Google search

It's now official! Google has integrated its social network Google+ into Google search result. The feature is quite aptly called Google Plus Your World.


Back in August, I wrote a short prediction that this would happen. It certainly raises some eyebrows up. Facebook, for one, would be in serious disadvantage if "Your World" included popular celebrities that one isn't linked to but is "naturally" shown in search result nonetheless. A really strong cross promotion bridge here. And of course, regulators should be closely watching the development of this feature.

And I also am interested in seeing how Google avert future anti-trust on its monopoly in search. It would make for a very smart business maneuver.

Hello, 2012

Awesome! A new year has come! Let's kick it into another great one.

And may all your resolutions be fulfilled.