TransactionalIBatch for Python?

%3CLINGO-SUB%20id%3D%22lingo-sub-1436369%22%20slang%3D%22en-US%22%3ETransactionalIBatch%20for%20Python%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1436369%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20noticed%20Azure%20has%20released%20transactional%20support%20across%20CosmosDB%20collections%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdevblogs.microsoft.com%2Fcosmosdb%2Fintroducing-transactionalbatch-in-the-net-sdk%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdevblogs.microsoft.com%2Fcosmosdb%2Fintroducing-transactionalbatch-in-the-net-sdk%2F%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20the%20same%20available%20in%20the%20Python%20SDK%3F%20How%20about%20via%20Python%20Azure%20functions%20and%20function%20bindings%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1469002%22%20slang%3D%22en-US%22%3ERe%3A%20TransactionalIBatch%20for%20Python%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1469002%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F687869%22%20target%3D%22_blank%22%3E%40Val26%3C%2FA%3E%26nbsp%3BHi%20there!%20Thanks%20for%20your%20great%20question.%20Unfortunately%2C%20I%20don't%20know%20the%20exact%20answer%20to%20this%20question%2C%20but%20I%20would%20recommend%20asking%20your%20question%20over%20at%20our%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fanswers%2Ftopics%2Fazure-functions.html%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Functions%20Q%26amp%3BA%20resource%3C%2FA%3E.%20We're%20directing%20people%20to%20this%20location%2C%20as%20it%20is%20frequently%20monitored%20by%20a%20host%20of%20very%20helpful%20people%20who%20may%20have%20the%20answers%20you%20are%20looking%20for.%20%3A)%3C%2Fimg%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESorry%20for%20not%20being%20able%20to%20help%20you%20further%20on%20this%20particular%20topic.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESkyler.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1470325%22%20slang%3D%22en-US%22%3ERe%3A%20TransactionalIBatch%20for%20Python%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1470325%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F625280%22%20target%3D%22_blank%22%3E%40skylerhartle%3C%2FA%3E%26nbsp%3B%2C%20thx%2C%20actually%20I%20have%20figured%20it%20out%20what%20seems%20to%20be%20a%20good%20solution.%20If%20I%20write%20the%20whole%20list%2C%20at%20once%2C%20to%20func.DocumentList%2C%20like%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Easync%20def%20upsert_posts(social_feed_writer%2C%20live_messages)%20-%26gt%3B%20List%5BPost%5D%3A%0A%20%20%20%20db_updates%20%3D%20func.DocumentList()%0A%20%20%20%20post_id%3D%20str(uuid.uuid4())%0A%0A%0A%20%20%20%20for%20live_message%20in%20live_messages%3A%0A%23some%20data%20conversion%20stuff%20here%0A%20%20%20%20%20%20%20%20post_from_live_message%20%3D%20live_message_to_post_dict(live_message%2C%20post_id)%0A%20%20%20%20%20%20%20%20post%20%3D%20Post.build_model(post_from_live_message)%0A%20%20%20%20%20%20%20%20post_data%20%3D%20post.to_data()%20%23makes%20a%20hash%0A%23here%20is%20appends%20a%20func.Document%20to%20me%20func.DocumentList%0A%20%20%20%20%20%20%20%20db_updates.append(func.Document.from_dict(post_data))%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20social_feed_writer.set(db_updates)%0A....%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EHere%20socialFeedWriter%20is%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3EsocialFeedWriter%3A%26nbsp%3Bfunc.Out%5Bfunc.DocumentList%5D%2C%20passed%20in%20binding%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECosmosDB%20binding%20in%20my%20Azure%20function%20seems%20to%20magically%20maintain%20a%20transaction%2C%20i.e.%20if%20one%20my%20entries%20fails%2C%20the%20rest%20will%20fail%20too%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1471207%22%20slang%3D%22en-US%22%3ERe%3A%20TransactionalIBatch%20for%20Python%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1471207%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F687869%22%20target%3D%22_blank%22%3E%40Val26%3C%2FA%3E%26nbsp%3BI'm%20glad%20you%20managed%20to%20figure%20out%20the%20problem%2C%20and%20thanks%20for%20posting%20the%20solution%20here%20for%20others%20to%20see.%26nbsp%3B%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Fhtml%2Fimages%2Femoticons%2Fsmile_40x40.gif%22%20alt%3D%22%3Asmile%3A%22%20title%3D%22%3Asmile%3A%22%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

I have noticed Azure has released transactional support across CosmosDB collections 

https://devblogs.microsoft.com/cosmosdb/introducing-transactionalbatch-in-the-net-sdk/

 

Is the same available in the Python SDK? How about via Python Azure functions and function bindings?

3 Replies

@Val26 Hi there! Thanks for your great question. Unfortunately, I don't know the exact answer to this question, but I would recommend asking your question over at our Azure Functions Q&A resource. We're directing people to this location, as it is frequently monitored by a host of very helpful people who may have the answers you are looking for. :)

 

Sorry for not being able to help you further on this particular topic.

 

Thanks!

 

Skyler.

 

@skylerhartle , thx, actually I have figured it out what seems to be a good solution. If I write the whole list, at once, to func.DocumentList, like

async def upsert_posts(social_feed_writer, live_messages) -> List[Post]:
    db_updates = func.DocumentList()
    post_id= str(uuid.uuid4())


    for live_message in live_messages:
#some data conversion stuff here
        post_from_live_message = live_message_to_post_dict(live_message, post_id)
        post = Post.build_model(post_from_live_message)
        post_data = post.to_data() #makes a hash
#here is appends a func.Document to me func.DocumentList
        db_updates.append(func.Document.from_dict(post_data))
        
    social_feed_writer.set(db_updates)
....

Here socialFeedWriter is 

socialFeedWriter: func.Out[func.DocumentList], passed in binding

 

CosmosDB binding in my Azure function seems to magically maintain a transaction, i.e. if one my entries fails, the rest will fail too

@Val26 I'm glad you managed to figure out the problem, and thanks for posting the solution here for others to see. :smile: