Before you start trying to optimize the program, you need to do something.
To get started, you must profile your programs. You can, for example, use line_profiler .
If it turns out that your software spends a significant amount of time registering, there are two simple options.
- Install loglevel in production code so that no or more (er) messages are logged. There will still be some overhead, but they should be significantly reduced.
- Use mechanical means (such as
sed or grep ) to completely remove registration calls from production code. If this does not improve the speed / throughput of your program, registration was not a problem.
If none of them works, and registration is a significant part of the time of your program, you can try to implement logging of threads or processes.
If you want to use threading for logging, you will need a list and a lock. The function called from the main thread for logging captures the lock, adds text to enter the list, and releases the lock. The second thread waits for the lock, captures the lock, pushes a couple of elements from the list, releases the lock, and writes the elements to a file. Since the GIL ensures that only one thread at a time uses Python bytecode, this will slightly decrease the performance of your program; part of its time is spent executing bytecode from the logging stream.
Using multiprocessing slightly different in that you probably want to use, for example. a Queue to send log messages from the main process to the logging process. The logging process takes items from the queue and writes them to disk. This means that the time taken to record the registration actions to disk is spent in another program. But there is some overhead associated with using the queue.
You will need to measure which method uses less time in your program.
Roland Smith
source share