Guest User

Untitled

a guest
Aug 1st, 2012
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. # -*- coding:utf-8 -*-
  2. # This demo demonstrates the concurrency problem of MongDB's GridFS interface.
  3.  
  4. import os, tempfile, multiprocessing, time
  5.  
  6. import pymongo, gridfs
  7. import mongoengine
  8.  
  9. DB_HOST = '127.0.0.1'
  10. DB_PORT = 27017
  11.  
  12. PROCESS_NUM = 2
  13.  
  14. class TestDoc(mongoengine.Document):
  15. field_a = mongoengine.IntField()
  16. field_b = mongoengine.StringField()
  17. field_c = mongoengine.DictField()
  18. field_fs = mongoengine.FileField()
  19.  
  20. def _conect_database():
  21. mongoengine.connect('TestDB', host=DB_HOST, port=DB_PORT)
  22.  
  23. def _create_database():
  24. connection = pymongo.Connection(DB_HOST, DB_PORT)
  25. db = connection['TestDB']
  26. connection.close()
  27.  
  28. _conect_database()
  29.  
  30. def _drop_database():
  31. connection = pymongo.Connection(DB_HOST, DB_PORT)
  32. connection.drop_database('TestDB')
  33.  
  34. def _write_file_field(obj_id):
  35. try:
  36. with tempfile.TemporaryFile() as f:
  37. # Create temporary file
  38. f.write("Hello XingCloud!\n" * 10)
  39. f.flush()
  40.  
  41. obj = TestDoc.objects.with_id(obj_id)
  42. file_name = 'PID_' + str(os.getpid())
  43.  
  44. f.seek(0)
  45. obj.field_fs.replace(f, filename=file_name)
  46. obj.save()
  47. except TestDoc.DoesNotExist as e:
  48. raise e
  49.  
  50. if __name__ == '__main__':
  51. _create_database()
  52.  
  53. # Crate document object
  54. print 'Create document object...'
  55. obj = TestDoc(
  56. field_a = 0,
  57. field_b = 'Hello XingCloud!',
  58. field_fs = '', # Without initial value, data cannot be written to
  59. # FieldFiled.
  60. )
  61. obj.save()
  62.  
  63. # Write GridFS field with multil-processes
  64. for i in xrange(PROCESS_NUM):
  65. multiprocessing.Process(
  66. target = _write_file_field,
  67. args = (obj.id, ),
  68. ).start()
  69.  
  70. time.sleep(1)
  71. print 'Writing finished...'
  72.  
  73. obj.reload()
  74.  
  75. print '>>> Doc:'
  76. print ' ID: %s' % obj.id
  77. print ' File: %s' % obj.field_fs.grid_id
  78.  
  79. connection = pymongo.Connection(DB_HOST, DB_PORT)
  80. db = connection['TestDB']
  81. grid_fs = gridfs.GridFS(db)
  82.  
  83. print '>>> GridFS:'
  84. print grid_fs.list()
  85.  
  86. _drop_database()
Advertisement
Add Comment
Please, Sign In to add comment