[Python] How to achieve this: after function execution, everything gets restored to the state...

Discussão em 'Python' iniciado por Stack, Dezembro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I found myself occasionally need to do "probe" version of a function.

    As a simple example, when developing evaluator, I might want to do a tentative evaluation to see whether current dependency info is enough or not. In this case, I'll call the eval function as if it is a probe, i.e., I only care its return value of bool that indicates succ/fail. Most importantly, as this is only a tentative attempt, I would like to keep everything intact throughout the execution, in other words, no data such as dependency/counter/memoi/etc is different before and after the probing.

    Now, if the probe function is simple enough, certainly I can make a new function mimicking the old one except for that every write operation is instead made on a deepcopy, etc.

    However, if the function has some thousand lines of code with logic so intertwined. Writing a new one is no longer practical. I can only rely on regression system to help identify as many issues as possible and bear the potential risk.

    I hence wonder

    • Is there any well defined way to achieve that?
    • Can fork be used to implement it on Linux? that is, parent proc simply do nothing but wait for child proc, child does the probe function and pass target value to parent by pipe. If my memory is not rusted so badly, fork uses copy-on-write scheme, it hence appeals to resemble the flow discussed above, and I'll expect the overhead to be relatively low. Any hazard doing it?

    EDIT:

    Fellows, I am still here expecting some comments and teaching. I noticed there're some votes to close this question, If the feature I asked for looks unreasonable, I would like to here about why, is it my design pattern somewhere so wrong so that I have to do this unnecessary attempt? Any way I can avoid this path? Or did I misunderstand or miss anything?

    Also, it just occurred to me that some functions may also have socket connections, say for the evaluator example, a Redis connection is established and will record the writing style of different users, say, what proportion of users write RPN (reverse polish notation), etc, to help further optimization. I think fork scheme cannot handle this. But let's just ignore and assume there's no network socket involved:)

    Continue reading...

Compartilhe esta Página