[PYTHON] How to write to update Datastore to async with Google Apps Engine

I wrote what I wanted to say in the title. Python2.7

from google.appengine.ext import ndb

class TestModel(ndb.Model):
    name = ndb.StringProperty()

@ndb.transactional_tasklet
def txn (key):
    entity = yield key.get_async()
    if not entity:
      entity = TestModel(key=key)
    entity.populate(key=key, name='foo')
    yield entity.put_async()
    raise ndb.Return(entity)

def put_multi_async(keys):
    @ndb.tasklet
    def _put_multi_async(keys):
        results = yield [txn(key) for key in keys]
        raise ndb.Return(results)

    return _put_multi_async(keys).get_result()

def main():
  key1 = ndb.Key(TestModel, 1)
  key2 = ndb.Key(TestModel, 2)
  key3 = ndb.Key(TestModel, 3)

  res = put_multi_async([key1, key2, key3])
  print res

main()

'''
=>
[TestModel(key=Key('TestModel', 1), name=u'foo'),
 TestModel(key=Key('TestModel', 2), name=u'foo'),
 TestModel(key=Key('TestModel', 3), name=u'foo')]
'''

Recommended Posts

How to write to update Datastore to async with Google Apps Engine
How to update with SQLAlchemy?
Python: How to use async with
How to update Google Sheets from Python
How to search Google Drive with Google Colaboratory
How to write async and await in Vue.js
[TF] How to specify variables to update with Optimizer
How to update Spyder
How to load files in Google Drive with Google Colaboratory
How to analyze with Google Colaboratory using Kaggle API
How to use Django on Google App Engine / Python
How to update Python Tkinter to 8.6
How to enable Read / Write of net.Conn with context with golang
How to cast with Theano
How to do Bulk Update with PyMySQL and notes [Python]
How to Alter with SQLAlchemy?
Write to csv with Python
How to separate strings with','
How to RDP with Fedora31
How to update FC2 blog etc. using XMLRPC with python
How to deal with UnicodeDecodeError when executing google image download
How to Delete with SQLAlchemy?
How to write offline real-time Solving E05 problems with Python
How to use Google Colaboratory
When using ancestor query with Google Apps Engine, parent does not have to use real entity
How to connect to Cloud Firestore from Google Cloud Functions with python code
How to write a docstring to create a named tuple document with sphinx
How to write offline real time Solve F01 problems with Python
How to cancel RT with tweepy
How to write a Python class
[Rails] How to display Google Map
[Python] Write to csv file with Python
How to write soberly in pandas
How to update Spyder in Anaconda
How to deal with imbalanced data
How to install python-pip with ubuntu20.04LTS
How to deal with imbalanced data
Google App Engine development with Docker
How to get started with Scrapy
How to get started with Python
Write FW to TWE-Lite with TWE-Lite R.
How to deal with DistributionNotFound errors
How to get started with Django
How to Data Augmentation with PyTorch
How to use FTP with Python
How to write Docker base image
How to calculate date with python
How to write Django1.9 environment-independent wsgi.py
How to install mysql-connector with pip3
How to INNER JOIN with SQLAlchemy
Notes on how to write requirements.txt
How to install Anaconda with pyenv
How to authenticate with Django Part 2
How to authenticate with Django Part 3
The 16th offline real-time how to write problem was solved with Python
Think about how to write a filter with the Shotgun API-Contact Versions
How to write offline real time I tried to solve E11 with python
The 16th offline real-time how to write reference problem to solve with Python
[Introduction to Python] How to write a character string with the format function
The 19th offline real-time how to write reference problem to solve with Python
The 15th offline real-time how to write problem was solved with python