Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72286

Re: Script suddenly stops

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'memory.': 0.07; 'suddenly': 0.07; '#print': 0.09; 'below).': 0.09; 'cursor': 0.09; 'except:': 0.09; 'host,': 0.09; 'iterate': 0.09; 'lines:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'variable,': 0.09; 'python': 0.11; 'def': 0.12; 'random': 0.14; 'bit.': 0.16; 'declarations': 0.16; 'len(line)': 0.16; 'query,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'throw': 0.16; 'true:': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; 'advance.': 0.19; 'import': 0.22; 'print': 0.22; 'header :User-Agent:1': 0.23; 'looks': 0.24; '(or': 0.24; 'script': 0.25; '(see': 0.26; 'skip:" 20': 0.27; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'host': 0.29; 'chris': 0.29; '[1]': 0.29; 'skip:( 40': 0.30; 'statement': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'lines': 0.31; 'file': 0.32; 'running': 0.33; 'skip:# 10': 0.33; 'table': 0.34; 'maybe': 0.34; 'could': 0.34; 'except': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'processed': 0.36; 'should': 0.36; 'skip:- 20': 0.37; 'starting': 0.37; 'thank': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'deleting': 0.60; 'break': 0.61; 'new': 0.61; 'first': 0.61; 'such': 0.63; 'valuable': 0.63; 'dear': 0.65; 'lose': 0.68; 'skip:r 30': 0.69; 'user,': 0.69; 'again?': 0.84; 'bare': 0.84; 'glance': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Script suddenly stops
Date Fri, 30 May 2014 09:26:57 +0200
Organization None
References <5387F12E.101@arcor.de>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bdbd86.dip0.t-ipconnect.de
User-Agent KNode/4.11.5
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10479.1401434831.18130.python-list@python.org> (permalink)
Lines 96
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1401434831 news.xs4all.nl 2839 [2001:888:2000:d::a6]:33383
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72286

Show key headers only | View raw


Chris wrote:

> Dear All,
> 
> I'm trying to read ten 200 MB textfiles into a MySQL MyISAM database
> (Linux, ext4). The script output is suddenly stopping, while the Python
> process is still running (or should I say sleeping?). It's not in top,
> but in ps visible.
> 
> Why is it stopping? Is there a way to make it continue, without calling
> "kill -9", deleting the processed lines and starting it again?
> 
> Thank you in advance.
> 
> 
> 
> [1] http://pastebin.com/CxHCA9eB
> 

> #!/usr/bin/python
>  
> import MySQLdb, pprint, re
> db = None
> daten = "/home/chris/temp/data/data/"
> host = "localhost"
> user = "data"
> passwd = "data"
> database = "data"
> table = "data"
>  
> def connect_mysql():
>     global db, host, user, passwd, database
>     db = MySQLdb.connect(host, user, passwd, database)
>     return(db)
>  
>  
> def read_file(srcfile):
>     lines = []
>     f = open(srcfile, 'r')
>     while True:
>         line = f.readline()
>         #print line
>         lines.append(line)
>         if len(line) == 0:
>             break
>     return(lines)

The read_file() function looks suspicious. It uses a round-about way to read 
the whole file into memory. Maybe your system is just swapping? 

Throw read_file() away and instead iterate over the file directly (see 
below).

> def write_db(anonid, query, querytime, itemrank, clickurl):
>     global db, table
>     print "write_db aufgerufen."
>     cur = db.cursor()
>     try:    
>         cur.execute("""INSERT INTO data 
(anonid,query,querytime,itemrank,clickurl) VALUES (%s,%s,%s,%s,%s)""",
(anonid,query,querytime,itemrank,clickurl))
>         db.commit()
>     except:
>         db.rollback()
>  
>  
> def split_line(line):
>     print "split_line called."
>     print "line is:", line
>     searchObj = re.split(r'(\d*)\t(.*)\t([0-9: -]+)\t(\d*)\t([A-Za-
z0-9._:/ -]*)',line, re.I|re.U)
>     return(searchObj)
>  
>  
>  
> db = connect_mysql()
> pprint.pprint(db)

with open(daten + "test-07b.txt") as lines:
    for line in lines:
        result = split_line(line)
        write_db(result[1], result[2], result[3], result[4], result[5])

> db.close()

Random remarks:

- A bare except is evil. You lose valuable information.
- A 'global' statement is only needed to rebind a module-global variable,
  not to access such a variable. At first glance all your 'global'
  declarations seem superfluous.
- You could change the signature of write_db() to accept result[1:6].
- Do you really need a new cursor for every write? Keep one around as a
  global.
- You might try cur.executemany() to speed things up a bit.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Script suddenly stops Peter Otten <__peter__@web.de> - 2014-05-30 09:26 +0200

csiph-web