[Python-Dev] pyparallel and new memory API discussions...
Charles-François Natali
cf.natali at gmail.com
Wed Jun 19 16:01:49 CEST 2013
2013/6/19 Trent Nelson <trent at snakebite.org>:
>
> The new memory API discussions (and PEP) warrant a quick pyparallel
> update: a couple of weeks after PyCon, I came up with a solution for
> the biggest show-stopper that has been plaguing pyparallel since its
> inception: being able to detect the modification of "main thread"
> Python objects from within a parallel context.
>
> For example, `data.append(4)` in the example below will generate an
> AssignmentError exception, because data is a main thread object, and
> `data.append(4)` gets executed from within a parallel context::
>
> data = [ 1, 2, 3 ]
>
> def work():
> data.append(4)
>
> async.submit_work(work)
>
> The solution turned out to be deceptively simple:
>
> 1. Prior to running parallel threads, lock all "main thread"
> memory pages as read-only (via VirtualProtect on Windows,
> mprotect on POSIX).
>
> 2. Detect attempts to write to main thread pages during parallel
> thread execution (via SEH on Windows or a SIGSEGV trap on POSIX),
> and raise an exception instead (detection is done in the ceval
> frame exec loop).
Quick stupid question: because of refcounts, the pages will be written
to even in case of read-only access. How do you deal with this?
cf
More information about the Python-Dev
mailing list