How to "fork off" a function

Thomas Wouters thomas at xs4all.nl
Sun Jul 2 03:31:28 EDT 2000


On Sun, 2 Jul 2000 08:01:50 +0200, Morten W. Petersen <morten at src.no> wrote:
>How do I "get rid of" an annoyingly slow function, that's more of a
>logging facility, in the code? Do I use thread, etc?

>Short example would be appreciated.

Well, this ought to work:

def long_function(*lots_of_args):
    # <original code commented out> 
    pass

Or isn't that what you wanted ? :-) Seriously, this is the only way to get
rid of a function without knowing that it's supposed to do. If the function
is a logging function, try to shortcut your way out of the function for the
cases where it isn't necessary. Or turn off logging. Or defer logging until
later, by appending log messsages to a list and writing out the list later.

You can use threads, yes, but it only makes sense if the logging function is
slow because it waits for something else, not if it takes a lot of cpu time.
I'd go for a seperate process and a syslogd-like daemon that listens for
messages, though: easier to write and much more portable.


lambda:0-ly y'rs,
Thomas.




More information about the Python-list mailing list