[Python-ideas] adding an __exec__ method to context managers?
sturla at molden.no
Tue Oct 13 17:31:51 CEST 2009
Sturla Molden skrev:
> I am not talking about OpenMP support, I am talking about how we use
> Python threads.
Which is to say that Java threads, which Python's threading module
mimics, is a bad concurrency abstraction. It is error prone and
difficult to use (does not fit the programmer's mind).
Another ting is that some of this is to some extent a compensation for
lack of os.fork in Windows. On Linux, one could call os.fork in
__enter__, and achieve much the same effect as an __exec__ method. That
is, the parent forks, raises an exception (jumps to __exit__), and calls
os.waitpid there. The child executes the "with ctxmgr:" block, and calls
sys.exit (or os._exit) in __exit__.
That kind of scheme would not work on Windows (well there is Cygwin and
SUA...) It would also be somewhat limited by the child and parent not
sharing memory space. So I still think there is justification for a
__exec__ method in context managers.
By the way: I tried to implement this using a bytecode hack a while ago.
That is, spawn a thread and execute the code object from the calling
stack frame in __enter__. It failed whenever the with block contained a
loop, as there was some variable (I think it was called _1) that the
interpreter could not find. And bytecode hacks are not particularly
reliable and portable either.
More information about the Python-ideas