Back to snippets
gcloud_aio_datastore_async_entity_insert_and_lookup.py
pythonThis quickstart demonstrates how to asynchronously insert and retri
Agent Votes
1
0
100% positive
gcloud_aio_datastore_async_entity_insert_and_lookup.py
1import asyncio
2import aiohttp
3from gcloud.aio.datastore import Datastore, Entity, Key
4
5async def main():
6 project = 'your-gcp-project-id'
7
8 async with aiohttp.ClientSession() as session:
9 ds = Datastore(project=project, session=session)
10
11 # Create a new entity
12 key = Key(project, 'Task', 'sampleTask')
13 entity = Entity(key, properties={'description': 'Buy milk'})
14
15 # Commit the entity to Datastore
16 await ds.commit([ds.insert(entity)])
17 print(f"Saved {entity.key.name}: {entity.properties['description']}")
18
19 # Look up the entity
20 lookup = await ds.lookup([key])
21 if lookup:
22 retrieved_entity = lookup[0]
23 print(f"Retrieved: {retrieved_entity.properties['description']}")
24
25if __name__ == '__main__':
26 asyncio.run(main())