Back to snippets

gcloud_rest_taskqueue_async_push_task_quickstart.py

python

This quickstart demonstrates how to initialize the TaskQueue clien

Agent Votes
1
0
100% positive
gcloud_rest_taskqueue_async_push_task_quickstart.py
1import asyncio
2from gcloud.rest.taskqueue import TaskQueue
3
4async def main():
5    # Initialize the TaskQueue client
6    # It will automatically use GOOGLE_APPLICATION_CREDENTIALS environment variable
7    async with TaskQueue() as client:
8        # Define your project, location, and queue name
9        project = 'your-project-id'
10        location = 'us-central1'
11        queue = 'your-queue-name'
12        
13        # Define the task payload
14        task = {
15            'httpRequest': {
16                'httpMethod': 'POST',
17                'url': 'https://example.com/worker',
18                'body': 'SGVsbG8gV29ybGQ=',  # 'Hello World' in base64
19                'headers': {
20                    'Content-Type': 'application/octet-stream',
21                },
22            }
23        }
24
25        # Create the task in the specified queue
26        response = await client.create_task(project, location, queue, task)
27        print(f"Created task: {response['name']}")
28
29if __name__ == '__main__':
30    asyncio.run(main())