Search code examples
pythonnginxgeventpymysql

how to read a textfile with python gevent


I have tried to read a nginx log file with python, but the log file is too big.

Can I read the file by gevent?

BTW , The log file has one million lines Can I store the data into mysqlwith pysqml api?


Solution

  • I'm posting my comment as an answer just to illustrate what I mean: why not just read the file line by line and insert the data as you go?

    import pymysql
    conn = pymysql.connect(host='localhost', port=####, user='userid', passwd='password', db='db')
    cursor = cur = conn.cursor()
    insert_cmd = "INSERT INTO logs (param1, param2, param3...) VALUES (%s, %s, %s...)"
    with open('logfile.log', 'r') as log:
        for row in log:
            cursor.execute(insert_cmd, tuple(row))
    conn.commit()