Monday, October 2, 2023
No Result
View All Result
Get the latest A.I News on A.I. Pulses
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing
No Result
View All Result
Get the latest A.I News on A.I. Pulses
No Result
View All Result

Unleashing the Energy of Python Asyncio’s Queue | by Peng Qian | Jun, 2023

June 7, 2023
144 6
Home Data science
Share on FacebookShare on Twitter


The queue talked about earlier is a First-In-First-Out (FIFO) queue, the place the primary merchandise to enter the queue is the primary to be retrieved. That is appropriate when all duties within the queue have the identical precedence.

Nevertheless, take into account the next scenario:

Suppose there’s a queue with duties ready in line, every requiring a protracted processing time.

An error log or VIP consumer entry is a high-priority job that wants rapid consideration. What ought to we do?

Picture by Ethan Hu on Unsplash

That is the place asyncio.PriorityQueue comes into play.

Briefly describe asyncio.PriorityQueue’s implementation

Not like FIFO queues based mostly on lists, asyncio.PriorityQueue is predicated on heaps. It’s constructed utilizing a binary tree construction.

You might be acquainted with binary search timber, which be sure that essentially the most minor node is at all times the leftmost node.

Nevertheless, the binary tree in asyncio.PriorityQueue ensures that essentially the most minor node is at all times on the high, so the very best precedence node is completely eliminated first.

On the left is the binary tree used by PriorityQueue, and on the right is the binary search tree.
On the left is the binary tree utilized by PriorityQueue, and on the precise is the binary search tree. Picture by Writer

Actual-world instance with asyncio.PriorityQueue

Let’s illustrate the utilization of asyncio.PriorityQueue with a real-world state of affairs that exists in apply.

Think about we now have an order service API. The API takes time for every order to course of, however we will’t maintain customers ready too lengthy.

So when a consumer locations an order, the API first places the order right into a queue, permitting a background job to course of it asynchronously whereas instantly returning a message to the consumer.

This API accepts orders from two kinds of customers: common customers and VIP customers. It should be sure that VIP consumer orders are processed with the very best precedence.

VIP orders are processed with the highest priority.
VIP orders are processed with the very best precedence. Picture by Writer

To maintain the training curve low for readers, on this instance, we are going to use aiohttp to implement the server. The precise code is as follows:

First, we outline an enumeration marking the 2 classes: common customers and VIP customers.

Subsequent, we use dataclass to outline a consumer’s order, which accommodates the consumer kind and order processing period. The order period isn’t thought of in precedence sorting.

Then we outline the patron methodology process_order_worker, which retrieves orders from the queue and simulates the order processing.

Don’t neglect to make use of queue.task_done() to inform the queue that we completed processing the order.

Following that, we implement the order API utilizing aiohttp. This API responds to consumer requests, generates an order object, and locations it within the asyncio.PriorityQueue.

It then instantly returns a response to the consumer, avoiding consumer wait time.

When this system begins, we use create_order_queue to initialize the queue and order consumption duties.

When this system ends, we use destroy_order_queue to make sure that all orders within the queue are processed and the background duties are closed accurately.

queue.be a part of() will watch for all the info within the queue to be processed. asyncio.wait_for units a timeout of 20 seconds, after which it should now not wait queue.be a part of() to finish.

We are able to take a look at this implementation utilizing PyCharm’s HTTP Request:

API prioritizes orders from VIP users whenever possible.
API prioritizes orders from VIP customers each time potential. Picture by Writer

As you possibly can see, the 2 high-priority duties are processed as anticipated. Good!



Source link

Tags: AsynciosJunPengPowerPythonQianQueueUnleashing
Next Post

The Golden Age of Open Supply in AI Is Coming to an Finish | by Clemens Mewald | Jun, 2023

Bridging the hole between studying and reasoning

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent News

Getting Began with Google’s Palm API Utilizing Python

October 2, 2023

Google at ICCV 2023 – Google Analysis Weblog

October 2, 2023

Researchers from China Introduce DualToken-ViT: A Fusion of CNNs and Imaginative and prescient Transformers for Enhanced Picture Processing Effectivity and Accuracy

October 2, 2023

Utilized AI – Future Potential and Practicality of AI in Healthcare with Mr. Manas Joshi

October 2, 2023

Modern Acoustic Swarm Expertise Shapes the Way forward for In-Room Audio

October 2, 2023

Getting Began with Google Cloud Platform in 5 Steps

October 2, 2023

Categories

  • A.I News
  • A.I. Startups
  • Computer Vision
  • Data science
  • Machine learning
  • Natural Language Processing
  • Robotics
A.I. Pulses

Get The Latest A.I. News on A.I.Pulses.com.
Machine learning, Computer Vision, A.I. Startups, Robotics News and more.

Categories

  • A.I News
  • A.I. Startups
  • Computer Vision
  • Data science
  • Machine learning
  • Natural Language Processing
  • Robotics
No Result
View All Result

Recent News

  • Getting Began with Google’s Palm API Utilizing Python
  • Google at ICCV 2023 – Google Analysis Weblog
  • Researchers from China Introduce DualToken-ViT: A Fusion of CNNs and Imaginative and prescient Transformers for Enhanced Picture Processing Effectivity and Accuracy
  • Home
  • DMCA
  • Disclaimer
  • Cookie Privacy Policy
  • Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2022 A.I. Pulses.
A.I. Pulses is not responsible for the content of external sites.

No Result
View All Result
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing

Copyright © 2022 A.I. Pulses.
A.I. Pulses is not responsible for the content of external sites.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In