Home  >  Q&A  >  body text

python3.x - How can python3 use multiple processes to write logs to the same log more safely?

I am doing a schedule and need to use multi-threading to call up the stored procedure or SQL in the program and record the log, and record the execution information in the log. However, according to the information, logging is multi-process safe and multi-thread unsafe. Is there any way to write logs to the same log file in multiple threads? thank you all

某草草某草草2702 days ago685

reply all(2)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-27 17:41:32

    It is recommended to use the producer-consumer model. The producer writes logs to the queue through multiple threads, and the consumer takes the logs from the queue and writes them into the log

    reply
    0
  • 高洛峰

    高洛峰2017-05-27 17:41:32

    Is it multi-process or multi-thread?

    If there are multiple processes, it is recommended to write to stderr, and then the manager (such as systemd or supervisord) can write the file uniformly.

    reply
    0
  • Cancelreply