Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pickling objects that contain umongo documents. #337

Open
jbkoh opened this issue Jan 15, 2021 · 0 comments
Open

Pickling objects that contain umongo documents. #337

jbkoh opened this issue Jan 15, 2021 · 0 comments

Comments

@jbkoh
Copy link

jbkoh commented Jan 15, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant