Nir Soffer
2022-Feb-01 14:20 UTC
[Libguestfs] [PATCH nbdkit] plugins: python: Make threading model explicit
The default threading model is already serializing all requests, but implementing thread_model() gives us good place to explain why the code works and why we don't use os.preadv and os.pwritev. --- plugins/python/examples/error.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/plugins/python/examples/error.py b/plugins/python/examples/error.py index 0331a83e..5cb368d2 100644 --- a/plugins/python/examples/error.py +++ b/plugins/python/examples/error.py @@ -11,33 +11,41 @@ # ./nbdkit -f -v python ./plugins/python/examples/error.py file=test.img # # Or run it after installing nbdkit like this: # # nbdkit -f -v python ./plugins/python/examples/error.py file=test.img # # The -f -v arguments are optional. They cause the server to stay in # the foreground and print debugging, which is useful when testing. import os +import nbdkit API_VERSION = 2 filename = None calls = 0 def config(key, value): global filename assert key == "file" filename = value +def thread_model(): + # Serialize all requests so we can seek safely in pread and pwrite + # and be compatible with python 3.6. In python 3.7 we can use + # os.preadv and os.pwritev and use the parallel threading model. + return nbdkit.THREAD_MODEL_SERIALIZE_ALL_REQUESTS + + def open(readonly): flags = os.O_RDONLY if readonly else os.O_RDWR return {"fd": os.open(filename, flags)} def can_extents(h): return True def get_size(h): -- 2.34.1
Eric Blake
2022-Feb-01 15:39 UTC
[Libguestfs] [PATCH nbdkit] plugins: python: Make threading model explicit
On Tue, Feb 01, 2022 at 04:20:29PM +0200, Nir Soffer wrote:> The default threading model is already serializing all requests, but > implementing thread_model() gives us good place to explain why the code > works and why we don't use os.preadv and os.pwritev.And protects us if we ever relax the python plugin to default to a more parallel model (although more likely, we would make it where we still default to serial, but clients can opt in to more parallel, the way we did when we added parallel support to the sh bindings)> > +def thread_model(): > + # Serialize all requests so we can seek safely in pread and pwrite > + # and be compatible with python 3.6. In python 3.7 we can use > + # os.preadv and os.pwritev and use the parallel threading model. > + return nbdkit.THREAD_MODEL_SERIALIZE_ALL_REQUESTSACK -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org