Movatterモバイル変換


[0]ホーム

URL:


[Python-Dev] xreadlines : readlines :: xrange : range

Tim Peterstim.one@home.com
Mon, 8 Jan 2001 23:29:02 -0500


[Andrew Kuchling]I'll chop everything except while_readline (which is most affected by thisstuff):> Linux: w/o USE_MS_GETLINE_HACK> while_readline        0.184  0.180>> Linux w/ USE_MS_GETLINE_HACK:> while_readline        0.183  0.190>> Solaris w/o USE_MS_GETLINE_HACK:> while_readline        0.839  0.840>> Solaris w/ USE_MS_GETLINE_HACK:> while_readline        0.769  0.770So it's probably a wash.  In that case, do we want to maintain two hacks forthis?  I can't use the FLOCKFILE/etc approach on Windows, while "theWindows" approach probably works everywhere (although its speed relies onthe platform factoring out at least the locking/unlocking in fgets).Both methods lack a refinement I would like to see, but can't achieve in"the Windows way":  ensure that consistency is on no worse than a per-linebasis.  Right now, both methods lock/unlock the file only for the extent ofthe current buffer size, so that two threads *can* get back differentinterleaved pieces of a single long line.  Like so:import threaddef read(f):    x = f.readline()    print "thread saw " + `len(x)` + " chars"    m.release()f = open("ga", "w") # a file with one long linef.write("x" * 100000 + "\n")f.close()m = thread.allocate_lock()for i in range(10):    print i    f = open("ga", "r")    m.acquire()    thread.start_new_thread(read, (f,))    x = f.readline()    print "main saw " + `len(x)` + " chars"    m.acquire(); m.release()    f.close()Here's a typical run on Windows (current CVS Python):0main saw 95439 charsthread saw 4562 chars1main saw 97941 charsthread saw 2060 chars2thread saw 43801 charsmain saw 56200 chars3thread saw 8011 charsmain saw 91990 chars4main saw 46546 charsthread saw 53455 chars5thread saw 53125 charsmain saw 46876 chars6main saw 98638 charsthread saw 1363 chars7main saw 72121 charsthread saw 27880 chars8thread saw 70031 charsmain saw 29970 chars9thread saw 27555 charsmain saw 72446 charsSo, yes, it's threadsafe now:  between them, the threads always see a grandtotal of 100001 characters.  But what friggin' good is that <wink>?  If,e.g., Guido wants multiple threads to chew over his giant logfile, there'sno guarantee that .readline() ever returns an actual line from the file.Not that Python 2.0 was any better in this respect ...


[8]ページ先頭

©2009-2026 Movatter.jp