<div dir="ltr"><br><br><div class="gmail_quote">On Sun Feb 22 2015 at 6:27:16 AM Michael Bayer <<a href="mailto:mbayer@redhat.com">mbayer@redhat.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
<br>
<br>
> On Feb 21, 2015, at 9:49 PM, Joshua Harlow <<a href="mailto:harlowja@outlook.com" target="_blank">harlowja@outlook.com</a>> wrote:<br>
><br>
> Some comments/questions inline...<br>
><br>
> Mike Bayer wrote:<br>
>><br>
>> Yuriy Taraday<<a href="mailto:yorik.sar@gmail.com" target="_blank">yorik.sar@gmail.com</a>> wrote:<br>
>><br>
>>>> On Fri Feb 20 2015 at 9:14:30 PM Joshua Harlow<<a href="mailto:harlowja@outlook.com" target="_blank">harlowja@outlook.com</a>> wrote:<br>
>>>> This feels like something we could do in the service manager base class,<br>
>>>> maybe by adding a "post fork" hook or something.<br>
>>> +1 to that.<br>
>>><br>
>>> I think it'd be nice to have the service __init__() maybe be something like:<br>
>>><br>
>>> def __init__(self, threads=1000, prefork_callbacks=None,<br>
>>> postfork_callbacks=None):<br>
>>> self.postfork_callbacks = postfork_callbacks or []<br>
>>> self.prefork_callbacks = prefork_callbacks or []<br>
>>> # always ensure we are closing any left-open fds last...<br>
>>> self.prefork_callbacks.append(<u></u>self._close_descriptors)<br>
>>> ...<br>
>>><br>
>>> (you must've meant postfork_callbacks.append)<br>
>>><br>
>>> Note that multiprocessing module already have `multiprocessing.util.<u></u>register_after_fork` method that allows to register callback that will be called every time a Process object is run. If we remove explicit use of `os.fork` in oslo.service (replace it with Process class) we'll be able to specify any after-fork callbacks in libraries that they need.<br>
>>> For example, EngineFacade could register `pool.dispose()` callback there (it should have some proper finalization logic though).<br>
>><br>
>> +1 to use Process and the callback system for required initialization steps<br>
>> and so forth, however I don’t know that an oslo lib should silently register<br>
>> global events on the assumption of how its constructs are to be used.<br>
>><br>
>> I think whatever Oslo library is responsible for initiating the Process/fork<br>
>> should be where it ensures that resources from other Oslo libraries are set<br>
>> up correctly. So oslo.service might register its own event handler with<br>
><br>
> Sounds like some kind of new entrypoint + discovery service that oslo.service (eck can we name it something else, something that makes it useable for others on pypi...) would need to plug-in to. It would seems like this is a general python problem (who is to say that only oslo libraries use resources that need to be fixed/closed after forking); are there any recommendations that the python community has in general for this (aka, a common entrypoint *all* libraries export that allows them to do things when a fork is about to occur)?<br>
><br>
>> oslo.db such that it gets notified of new database engines so that it can<br>
>> associate a disposal with it; it would do something similar for<br>
>> oslo.messaging and other systems that use file handles. The end<br>
>> result might be that it uses register_after_fork(), but the point is that<br>
>> oslo.db.sqlalchemy.create_<u></u>engine doesn’t do this; it lets oslo.service<br>
>> apply a hook so that oslo.service can do it on behalf of oslo.db.<br>
><br>
> Sounds sort of like global state/a 'open resource' pool that each library needs to maintain internally to it that tracks how applications/other libraries are using it; that feels sorta odd IMHO.<br>
><br>
> Wouldn't that mean libraries that provide back resource objects, or resource containing objects..., for others to use would now need to capture who is using what (weakref pools?) to retain what all the resources are being used and by whom (so that they can fix/close them on fork); not every library has a pool (like sqlalchemy afaik does) to track these kind(s) of things (for better or worse...). And what if those libraries use other libraries that use resources (who owns what?); seems like this just gets very messy/impractical pretty quickly once you start using any kind of 3rd party library that doesn't follow the same pattern... (which brings me back to the question of isn't there a common python way/entrypoint that deal with forks that works better than ^).<br>
><br>
>><br>
>> So, instead of oslo.service cutting through and closing out the file<br>
>> descriptors from underneath other oslo libraries that opened them, we set up<br>
>> communication channels between oslo libs that maintain a consistent layer of<br>
>> abstraction, and instead of making all libraries responsible for the side<br>
>> effects that might be introduced from other oslo libraries, we make the<br>
>> side-effect-causing library the point at which those effects are<br>
>> ameliorated as a service to other oslo libraries. This allows us to keep<br>
>> the knowledge of what it means to use “multiprocessing” in one<br>
>> place, rather than spreading out its effects.<br>
><br>
> If only we didn't have all those other libraries[1] that people use to (that afaik highly likely also have resources they open); so even with getting oslo.db and oslo.messaging into this kind of pattern, we are still left with the other 200+ that aren't/haven't been following this pattern ;-)<br>
<br>
I'm only trying to solve well known points like this one between two Oslo libraries. Obviously trying to multiply out this pattern times all libraries, including non Oslo ones, is infeasible.<br>
<br>
The issue here is simple. Does oslo.service have to worry that the app uses oslo.db also, or does oslo.db have to worry that the app uses oslo.service also? I say the former bc oslo.db is the more fundamental system at the base of an app, whereas oslo.service actually deploys code in a certain context.<br></blockquote><div><br></div><div>I think that oslo.db needs to worry about running in Python environment where people might want to create new Process'es. If we tie everything to oslo.service we won't be able to do the right thing if someone just wants to fork in some other code.</div><div>We can however provide some nice wrapper around `<span style="font-size:13.1999998092651px">register</span><span style="font-size:13.1999998092651px">_after_fork` that would allow us to avoid all the weakrefs in other libraries' code.</span></div></div></div>