Python sqlite3 and concurrency

前端 未结 14 765
南笙
南笙 2020-11-30 18:00

I have a Python program that uses the \"threading\" module. Once every second, my program starts a new thread that fetches some data from the web, and stores this data to my

14条回答
  •  有刺的猬
    2020-11-30 18:49

    I like Evgeny's answer - Queues are generally the best way to implement inter-thread communication. For completeness, here are some other options:

    • Close the DB connection when the spawned threads have finished using it. This would fix your OperationalError, but opening and closing connections like this is generally a No-No, due to performance overhead.
    • Don't use child threads. If the once-per-second task is reasonably lightweight, you could get away with doing the fetch and store, then sleeping until the right moment. This is undesirable as fetch and store operations could take >1sec, and you lose the benefit of multiplexed resources you have with a multi-threaded approach.

提交回复
热议问题