SQLite3 and Multiprocessing

前端 未结 4 1681
南笙
南笙 2021-01-05 09:26

I noticed that sqlite3 isn´t really capable nor reliable when i use it inside a multiprocessing enviroment. Each process tries to write some data into the same database, so

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-05 09:56

    I've actually just been working on something very similar:

    • multiple processes (for me a processing pool of 4 to 32 workers)
    • each process worker does some stuff that includes getting information from the web (a call to the Alchemy API for mine)
    • each process opens its own sqlite3 connection, all to a single file, and each process adds one entry before getting the next task off the stack

    At first I thought I was seeing the same issue as you, then I traced it to overlapping and conflicting issues with retrieving the information from the web. Since I was right there I did some torture testing on sqlite and multiprocessing and found I could run MANY process workers, all connecting and adding to the same sqlite file without coordination and it was rock solid when I was just putting in test data.

    So now I'm looking at your phrase "(fetching data from the web)" - perhaps you could try replacing that data fetching with some dummy data to ensure that it is really the sqlite3 connection causing you problems. At least in my tested case (running right now in another window) I found that multiple processes were able to all add through their own connection without issues but your description exactly matches the problem I'm having when two processes step on each other while going for the web API (very odd error actually) and sometimes don't get the expected data, which of course leaves an empty slot in the database. My eventual solution was to detect this failure within each worker and retry the web API call when it happened (could have been more elegant, but this was for a personal hack).

    My apologies if this doesn't apply to your case, without code it's hard to know what you're facing, but the description makes me wonder if you might widen your considerations.

提交回复
热议问题