保存到线程内的数据库

时间:2015-07-24 13:44:32

标签: python django multithreading sqlite queue

我正在使用django,

我有一个线程,其目的是获取数据库项的排队列表并修改它们

这是我的模特:

x = c(23, 23, 16, 16, 34, 34, 43, 43, 97, 97, 63, 63, 39, 39, 29, 
29, 63, 63, 48, 48, 7, 7, 80, 80, 69, 69, 110, 110, 103, 103, 
43, 43, 39, 39, 46, 46, 14, 14, 56, 56, 76, 76, 52, 52, 18, 18, 
32, 32, 66, 66, 70, 70, 26, 26, 40, 40, 105, 105, 62, 62, 51, 
51, 58, 58, 37, 37, 55, 55, 42, 42, 11, 11, 89, 89, 55, 55, 109, 
109, 49, 49, 27, 27, 96, 96, 27, 27, 65, 65, 74, 74, 17, 17, 
33, 33, 89, 89, 63, 63, 18, 18, 25, 25, 36, 36, 108, 108, 3, 
3, 52, 52, 83, 83, 74, 74, 56, 56, 99, 99, 6, 6, 25, 25, 51, 
51, 4, 4, 100, 100, 17, 17, 44, 44, 23, 23, 70, 70, 85, 85, 14, 
14, 22, 22, 89, 89, 45, 45, 2, 2, 29, 29, 14, 14, 69, 69, 96, 
96, 10, 10, 58, 58, 97, 97, 54, 54, 60, 60, 65, 65, 2, 2, 54, 
54, 4, 4, 28, 28, 107, 107, 74, 74, 72, 72, 71, 71, 42, 42, 92, 
92, 64, 64, 39, 39, 111, 111, 72, 72, 73, 73, 58, 58, 41, 41, 
56, 56, 73, 73, 18, 18, 73, 73, 36, 36, 60, 60, 49, 49, 47, 47, 
95, 95, 19, 19, 8, 8, 7, 7, 38, 38, 38, 38, 38, 38, 28, 28, 79, 
79, 53, 53, 30, 30, 19, 19, 14, 14, 53, 53, 68, 68, 39, 39, 42, 
42, 87, 87, 33, 33, 18, 18, 77, 77, 83, 83, 19, 19, 14, 14, 7, 
7, 32, 32, 94, 94, 30, 30, 55, 55, 89, 89, 30, 30, 45, 45, 84, 
84, 38, 38, 59, 59, 73, 73, 77, 77, 22, 22, 55, 55, 31, 31, 52, 
52, 20, 20, 26, 26, 62, 62, 55, 55, 46, 46, 26, 26, 49, 49, 22, 
22, 65, 65, 67, 67, 73, 73, 29, 29, 88, 88, 86, 86, 76, 76, 32, 
32, 12, 12, 19, 19, 14, 14, 8, 8, 63, 63, 63, 63, 65, 65, 84, 
84, 34, 34, 42, 42, 26, 26, 75, 75, 68, 68, 28, 28, 95, 95, 17, 
17, 76, 76, 33, 33, 91, 91, 93, 93, 80, 80, 89, 89, 64, 64, 81, 
81, 98, 98, 47, 47, 70, 70, 46, 46, 11, 11, 92, 92, 69, 69, 95, 
95, 51, 51, 87, 87, 61, 61, 50, 50, 47, 47, 35, 35, 31, 31, 39, 
39, 19, 19, 81, 81, 35, 35, 68, 68, 68, 68, 67, 67, 57, 57, 7, 
7, 9, 9, 23, 23, 50, 50, 89, 89, 41, 41, 54, 54, 53, 53, 57, 
57, 89, 89, 32, 32, 40, 40, 48, 48, 35, 35, 15, 15, 90, 90, 1, 
1, 17, 17, 53, 53, 73, 73, 76, 76, 59, 59, 45, 45, 68, 68, 21, 
21, 37, 37, 33, 33, 51, 51, 61, 61, 31, 31, 15, 15, 23, 23, 29, 
29, 45, 45, 96, 96, 87, 87, 37, 37, 104, 104, 50, 50, 58, 58, 
103, 103, 91, 91, 72, 72, 73, 73, 27, 27, 60, 60, 23, 23, 99, 
99, 28, 28, 78, 78, 27, 27, 82, 82, 63, 63, 34, 34, 84, 84, 62, 
62, 2, 2, 99, 99, 22, 22, 85, 85, 39, 39, 47, 47, 66, 66, 17, 
17, 74, 74, 45, 45, 70, 70, 87, 87, 28, 28, 97, 97, 89, 89, 33, 
33, 50, 50, 79, 79, 86, 86, 69, 69, 91, 91, 75, 75, 52, 52, 76, 
76, 13, 13, 71, 71, 42, 42, 20, 20, 28, 28, 56, 56, 69, 69, 16, 
16, 47, 47, 60, 60, 45, 45, 72, 72, 78, 78, 107, 107, 4, 4, 64, 
64, 88, 88, 9, 9, 3, 3, 10, 10, 92, 92, 41, 41, 5, 5, 35, 35, 
31, 31, 24, 24, 70, 70, 47, 47, 41, 41, 32, 32, 92, 92, 90, 90, 
75, 75, 3, 3, 78, 78, 30, 30, 93, 93, 60, 60, 17, 17, 25, 25, 
48, 48, 70, 70, 69, 69, 66, 66, 76, 76, 104, 104, 31, 31, 72, 
72, 56, 56, 64, 64, 92, 92, 68, 68, 102, 102, 100, 100, 27, 27, 
40, 40, 47, 47, 29, 29, 76, 76, 78, 78, 20, 20, 13, 13, 10, 10, 
113, 113, 17, 17, 61, 61, 69, 69, 65, 65, 16, 16, 100, 100, 5, 
5, 18, 18, 24, 24, 54, 54, 41, 41, 64, 64, 66, 66, 90, 90, 29, 
29, 97, 97, 37, 37, 42, 42, 84, 84, 37, 37, 74, 74, 65, 65, 12, 
12, 49, 49, 31, 31, 108, 108, 9, 9, 93, 93, 71, 71, 39, 39, 70, 
70, 79, 79, 92, 92, 60, 60, 104, 104, 79, 79, 103, 103, 38, 38, 
93, 93, 46, 46, 66, 66, 79, 79, 51, 51, 31, 31, 65, 65, 93, 93, 
25, 25, 22, 22, 91, 91, 123, 123, 51, 51, 34, 34, 64, 64, 31, 
31, 24, 24, 74, 74, 57, 57, 95, 95, 83, 83, 28, 28, 56, 56, 72, 
72, 43, 43, 18, 18, 66, 66, 32, 32, 17, 17, 67, 67, 10, 10, 44, 
44, 66, 66, 57, 57, 89, 89, 57, 57, 55, 55, 18, 18, 78, 78, 82, 
82, 103, 103, 110, 110, 92, 92, 54, 54, 35, 35, 8, 8, 53, 53, 
86, 86, 45, 45, 99, 99, 19, 19, 84, 84, 94, 94, 92, 92, 80, 80, 
69, 69, 45, 45, 22, 22, 59, 59, 9, 9, 41, 41, 72, 72, 24, 24, 
117, 117, 79, 79, 57, 57, 29, 29, 96, 96, 47, 47, 23, 23, 64, 
64, 33, 33, 48, 48, 80, 80, 30, 30, 42, 42, 10, 10, 42, 42, 68, 
68, 46, 46, 58, 58, 39, 39, 82, 82, 79, 79, 80, 80, 89, 89, 85, 
85, 24, 24, 106, 106, 40, 40, 90, 90, 69, 69, 92, 92, 84, 84, 
82, 82, 86, 86, 80, 80, 73, 73, 78, 78, 39, 39, 27, 27, 55, 55, 
100, 100, 63, 63, 21, 21, 46, 46, 94, 94, 6, 6, 45, 45, 66, 66, 
94, 94, 52, 52, 78, 78, 59, 59, 86, 86, 67, 67, 76, 76, 54, 54, 
47, 47, 37, 37, 76, 76, 32, 32, 49, 49, 87, 87, 122, 122, 27, 
27, 82, 82, 51, 51, 50, 50, 22, 22, 32, 32, 99, 99, 77, 77, 54, 
54, 29, 29, 82, 82, 80, 80, 85, 85, 30, 30, 57, 57, 41, 41, 50, 
50, 65, 65, 51, 51, 109, 109, 89, 89, 50, 50, 6, 6, 66, 66, 42, 
42, 48, 48, 88, 88, 67, 67, 89, 89, 109, 109, 80, 80, 64, 64, 
64, 64, 95, 95, 76, 76, 76, 76, 78, 78, 44, 44, 51, 51, 19, 19, 
29, 29, 31, 31, 75, 75, 11, 11, 10, 10, 64, 64, 80, 80, 29, 29, 
73, 73, 67, 67, 38, 38, 27, 27, 23, 23, 74, 74, 79, 79, 49, 49, 
78, 78, 29, 29, 59, 59, 70, 70, 8, 8, 24, 24, 39, 39, 80, 80, 
27, 27, 29, 29, 36, 36, 94, 94, 86, 86, 35, 35, 84, 84, 99, 99, 
83, 83, 92, 92, 81, 81, 58, 58, 2, 2, 64, 64, 75, 75, 29, 29, 
53, 53, 58, 58, 11, 11, 38, 38, 83, 83, 108, 108, 86, 86, 56, 
56, 12, 12, 84, 84, 76, 76, 38, 38, 54, 54, 37, 37, 27, 27, 61, 
61, 83, 83, 37, 37, 59, 59, 81, 81, 76, 76, 70, 70, 61, 61, 101, 
101, 77, 77, 68, 68, 74, 74, 83, 83, 70, 70, 93, 93, 53, 53, 
64, 64, 89, 89, 1, 1, 53, 53, 67, 67, 81, 81, 71, 71, 51, 51, 
85, 85, 35, 35, 67, 67, 53, 53, 37, 37, 31, 31, 65, 65, 82, 82, 
47, 47, 60, 60, 81, 81, 21, 21, 94, 94, 75, 75, 92, 92, 113, 
113, 93, 93, 84, 84, 77, 77, 82, 82, 84, 84, 58, 58, 83, 83, 
84, 84, 80, 80, 1, 1, 49, 49, 73, 73, 22, 22, 99, 99, 74, 74, 
28, 28, 33, 33, 74, 74, 91, 91, 83, 83, 70, 70, 99, 99, 69, 69, 
38, 38, 68, 68, 47, 47, 61, 61, 47, 47, 70, 70, 85, 85, 20, 20, 
100, 100, 3, 3, 49, 49, 100, 100, 85, 85, 54, 54, 8, 8, 3, 3, 
47, 47, 46, 46, 45, 45, 27, 27, 87, 87, 20, 20, 24, 24, 51, 51, 
50, 50, 105, 105, 73, 73, 13, 13, 18, 18, 51, 51, 75, 75, 55, 
55, 62, 62, 85, 85, 56, 56, 51, 51, 66, 66, 74, 74, 63, 63, 2, 
2, 81, 81, 85, 85, 19, 19, 16, 16, 83, 83, 36, 36, 79, 79, 63, 
63, 41, 41, 45, 45, 76, 76, 62, 62, 67, 67, 74, 74, 92, 92, 47, 
47, 41, 41, 80, 80, 57, 57, 100, 100, 66, 66, 58, 58, 65, 65, 
59, 59, 20, 20, 54, 54, 10, 10, 79, 79, 64, 64, 106, 106, 44, 
44, 28, 28, 41, 41, 49, 49, 80, 80, 61, 61, 20, 20, 75, 75, 59, 
59, 93, 93, 32, 32, 38, 38, 30, 30, 41, 41, 8, 8, 8, 8, 54, 54, 
56, 56, 83, 83, 81, 81, 77, 77, 42, 42, 59, 59, 11, 11, 21, 21, 
77, 77, 84, 84, 86, 86, 84, 84, 34, 34, 48, 48, 80, 80, 92, 92, 
18, 18, 66, 66, 40, 40, 45, 45, 60, 60, 80, 80, 2, 2, 5, 5, 84, 
84, 66, 66, 70, 70, 70, 70, 95, 95, 62, 62, 0, 0, 67, 67, 61, 
61, 71, 71, 73, 73, 82, 82, 45, 45, 54, 54, 43, 43)

和有问题的代码:

class MyModel(models.Model):
    boolean = models.BooleanField(editable=False)

我现在的代码缺少很多检查...我在这里简化了以使其更清晰

如果能够在线程内部保存到我的数据库,我该怎么办?

编辑:我需要同步,因为i.boolean(和我的实际代码中的其他属性)不能覆盖

我试图在数据库中创建一个专用的表,但它没有用,我仍然有同样的问题

编辑2:我应该准确地说我正在使用SQLite3。我试着看看我是否可以解锁/锁定SQLite中的特定表,而且看起来锁定仅适用于整个数据库。这可能就是为什么使用专用表没有帮助。

这对我不好,因为我需要从不同的线程同时访问不同的表,是否可能?

编辑3:我的问题似乎就是这里列出的问题 https://docs.djangoproject.com/en/1.8/ref/databases/#database-is-locked-errors

2 个答案:

答案 0 :(得分:0)

您确定需要同步队列吗?可能是异步解决方案会解决你的问题吗? Need a thread-safe asynchronous message queue

答案 1 :(得分:0)

我找到的解决方案是更改数据库,

SQLite不允许这样的concurency访问。

我切换到MySql,现在可以正常工作