Page tree
Skip to end of metadata
Go to start of metadata

python的LevelDB库,使用挺方便的。

write_batch

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import os
import hashlib
import base64
import plyvel
 
 
def main():
    db = plyvel.DB("./cache", create_if_missing=True)
    with db.write_batch() as wb:
        for i in xrange(100000):
            key = hashlib.md5(os.urandom(32)).hexdigest()
            val = base64.b64encode(os.urandom(1024))
            wb.put(key, val)
            if i % 10000 == 0:
                print i
 
 
if __name__ == "__main__":
    main()

对比:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import os
import hashlib
import base64
import plyvel
 
 
def main():
    db = plyvel.DB("./cache", create_if_missing=True)
    counter = 0
    with db.write_batch() as wb:
        for i in xrange(100000):
            key = hashlib.md5(os.urandom(32)).hexdigest()
            val = base64.b64encode(os.urandom(1024))
            wb.put(key, val)
            counter += 1
            if counter >= 1000:
                wb.write()
                counter = 0
 
            if i % 10000 == 0:
                print i
 
 
if __name__ == "__main__":
    main()


  • No labels
Write a comment...