Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Python reading file utf-8 uses a lot of memory

When I add utf-8 encoding it uses about 3000mb of memory (and freezes my PC) the exmaple below works fine,

Code
pool = ThreadPoolExecutor(max_workers=10)
with open("data.txt") as f:
        for line in f:
            pool.submit(thread, line)
pool.shutdown(wait=True)
but when I add utf-8 encoding it basically crashes my PC

Code
pool = ThreadPoolExecutor(max_workers=10)
with open("data.txt", encoding='utf8') as f:
        for line in f:
            pool.submit(thread, line)
pool.shutdown(wait=True)
i like python lua js
python 3 is unicode by default maybe its the reason
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(May 4, 2017 08:49 AM)HFDone Wrote:  python 3 is unicode by default maybe its the reason

well I get this error when I dont use encoding='utf8'

Code
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 854: character maps to <undefined>
i like python lua js
yea as i said its unicode problem try str(line, 'utf-8')
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(May 4, 2017 10:18 AM)HFDone Wrote:  yea as i said its unicode problem try str(line, 'utf-8')

like so?
Code
with open("data.txt") as f:
        for line in f:
            line = str(line, 'utf-8')
            pool.submit(thread, line)
pool.shutdown(wait=True)
i like python lua js
Is that file large? Because it will load the file into memory, so you will have to read the file and iterate over the chunks you want to keep the memory usage low.

Look into codecs: http://stackoverflow.com/questions/16130...-different
[Random signature]
(May 4, 2017 11:31 AM)Pillus Wrote:  Is that file large? Because it will load the file into memory, so you will have to read the file and iterate over the chunks you want to keep the memory usage low.

Look into codecs: http://stackoverflow.com/questions/16130...-different

yeah it's a very large file, I'll look at this
i like python lua js
(May 4, 2017 02:30 PM)Riԁdle Wrote:  yeah it's a very large file, I'll look at this

Yeah then that is the problem. A normal file would load the entire thing into memory, so the larger the file, the more memory usage :)
[Random signature]
(May 4, 2017 02:34 PM)Pillus Wrote:  Yeah then that is the problem. A normal file would load the entire thing into memory, so the larger the file, the more memory usage :)

thank you <3
i like python lua js
You're loading the file 10 times in memory, in your case it seems the file size is around 300mb, loading it 10 times ( one time in every worker ThreadPoolExecutor(max_workers=10) ) requires 3000mb of RAM usage.

I'd suggest either lowering your max_workers, or splitting the file to 10 parts.

Good luck :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by MiguelSanchezJun 5, 2022 10:47 PM 5 308 Jun 5, 2022 10:47 PM
Last Post: MiguelSanchez
[C] by Net.Hun73rDec 27, 2020 01:54 AM 5 287 Dec 27, 2020 01:54 AM
Last Post: 【BADMAN】