umongo icon indicating copy to clipboard operation
umongo copied to clipboard

Pickling objects that contain umongo documents.

Open jbkoh opened this issue 3 years ago • 0 comments

Hi there,

Thanks for the great project! I'm trying to replace mongoengine with umongo because of its awesome asyncio support. In that, I got a problem. I'd really appreciate any input here:

I'm trying to dump objects that contain umongo documents. I put an MRE here:

import pickle
import cloudpickle
import asyncio

from umongo import Document
from umongo.fields import StringField
from umongo import MotorAsyncIOInstance
from motor.motor_asyncio import AsyncIOMotorClient

instance = MotorAsyncIOInstance()

async def setup_mongo():
    db_name = 'testdb'
    mongourl = f"mongodb://localhost:27017/{db_name}"
    loop = asyncio.get_event_loop()
    conn = AsyncIOMotorClient(mongourl, io_loop=loop)
    db = conn.get_database(db_name)
    instance.init(db)

@instance.register
class TestDoc(Document):
    test_field = StringField()

async def ensure_indexes():
    await TestDoc.ensure_indexes()


class TestClass(object):
    def __init__(self, test_model):
        self.test_model = test_model
        self.test_data = 'abcd'

async def main():
    await setup_mongo()
    await ensure_indexes()

    test_doc = TestDoc(test_field= '1234')
    test_instance = TestClass(test_model=test_doc)
    print(pickle.dumps(test_instance))

if __name__ == '__main__':
    asyncio.run(main())

However, this says:

  File "test.py", line 39, in main
    print(pickle.dumps(test_instance))
_pickle.PicklingError: Can't pickle <class 'umongo.data_proxy.TestDocDataProxy'>: attribute lookup TestDocDataProxy on umongo.data_proxy failed

I also checked #167 saying that umongo has its own way of dumping and loading and pickling won't work. What would be the best way of resolving the above case?

Thanks in advance.

jbkoh avatar Jan 15 '21 01:01 jbkoh