Python Django“杀死”

时间:2017-02-02 23:14:57

标签: python django

新提交,在本地工作但不在生产中。消息很奇怪。

uwsgi一切都像往常一样开始。但是在我发送服务器的第一个命中,一切都冻结了(我的意思是一切,甚至SSH会话都停止响应)。最终其中一名工人死亡,服务器再次开始响应。 ./manage.py runserver会发生这种情况:

$ ./manage.py runserver
Performing system checks...

Killed

就是这样。提交中唯一的一行是views.py

的开头
import my_module

同样,这适用于本地但不适用于生产。我不能给你太多关于my_module的详细信息,但它基本上是一个非常长列表,其中一个方法可以进行二进制搜索。

我怀疑这与事情有多大有关。 my_module.py文件为62MB。

我怎样才能知道发生了什么?

编辑:尝试./manage.py runserver然后dmesg,两次:

[  372.911491] python[1692]: segfault at 24 ip 0000000000558077 sp 00007f6624b70880 error 6 in python2.7[400000+2bc000]
[  414.833167] python[1729]: segfault at 24 ip 0000000000558077 sp 00007f9f17cbc880 error 6 in python2.7[400000+2bc000]
[  414.837098] Core dump to |/usr/share/apport/apport 1726 11 0 1726 pipe failed

1 个答案:

答案 0 :(得分:0)

以下是如何使用sqlite解决问题的示例。搜索被编入索引,因此它们的速度与获取它们的速度一样快:

import os
import random
import sqlite3
import contextlib
from datetime import datetime


@contextlib.contextmanager
def get_connection():
    connection = sqlite3.connect('database.db')
    try:
        yield connection
        connection.commit()
    finally:
        connection.close()


@contextlib.contextmanager
def get_cursor():
    with get_connection() as connection:
        cursor = connection.cursor()
        try:
            yield cursor
        finally:
            cursor.close()


def initial_create():
    # read your giant array from a file here

    with get_cursor() as cursor:
        cursor.executescript('''
        BEGIN;
        CREATE TABLE big_list (
            a int,
            b int,
            value text
        );

        CREATE INDEX big_list__a_b_idx ON big_list (a, b);
        COMMIT;
        ''')

        for i in range(1000):
            a = random.randint(0, 1000000)
            inserts = []

            for j in range(1000):
                b = random.randint(0, 1000000)

                inserts.append((
                    a,
                    b,
                    'some string (%d,%d)' % (a, b),
                ))

            cursor.executemany('INSERT INTO big_list VALUES (?,?,?)', inserts)


def test():
    with get_cursor() as cursor:
        print 'Total rows: %d' % (
            cursor.execute('SELECT COUNT(*) FROM big_list').fetchone())

        print 'Exact searches:'
        for i in range(10):
            start = datetime.now()

            result = cursor.execute('''
            SELECT *
            FROM big_list
            WHERE a = %d
            AND b = %d
            ''' % (i * 1000, i * 10000))
            print 'Got results: %r in' % result.fetchall(),
            print datetime.now() - start

        print 'Ranged searches:'
        for i in range(10):
            start = datetime.now()

            result = cursor.execute('''
            SELECT *
            FROM big_list
            WHERE a BETWEEN %d AND %d
            AND b BETWEEN %d AND %d
            ''' % (i * 1000, (i + 1) * 1000, i * 1000, (i + 1) * 1000))
            print 'Got results: %r in' % result.fetchall(),
            print datetime.now() - start


if __name__ == '__main__':
    if not os.path.isfile('database.db'):
        print 'Creating database, this should only be needed once'
        initial_create()

    test()

示例输出:

Total rows: 1000000
Exact searches:
Got results: [] in 0:00:00.000113
Got results: [] in 0:00:00.000055
Got results: [] in 0:00:00.000044
Got results: [] in 0:00:00.000044
Got results: [] in 0:00:00.000043
Got results: [] in 0:00:00.000045
Got results: [] in 0:00:00.000043
Got results: [] in 0:00:00.000041
Got results: [] in 0:00:00.000044
Got results: [] in 0:00:00.000041
Ranged searches:
Got results: [(604, 31, u'some string (604,31)'), (604, 386, u'some string (604,386)')] in 0:00:00.000889
Got results: [(1142, 1856, u'some string (1142,1856)')] in 0:00:00.000538
Got results: [] in 0:00:00.000056
Got results: [(3802, 3983, u'some string (3802,3983)')] in 0:00:00.000482
Got results: [] in 0:00:00.000165
Got results: [] in 0:00:00.000047
Got results: [] in 0:00:00.000164
Got results: [(7446, 7938, u'some string (7446,7938)'), (7947, 7381, u'some string (7947,7381)')] in 0:00:00.000867
Got results: [(8003, 8174, u'some string (8003,8174)')] in 0:00:00.000501