Из-за периодической блокировки нашего сайта РКН сервисами, просим воспользоваться резервным адресом:
Загрузить через dTub.ru Загрузить через ClipSaver.ruУ нас вы можете посмотреть бесплатно How to Ensure Your HTTP Requests are Sent Asynchronously in Rust или скачать в максимальном доступном качестве, которое было загружено на ютуб. Для скачивания выберите вариант из формы ниже:
Роботам не доступно скачивание файлов. Если вы считаете что это ошибочное сообщение - попробуйте зайти на сайт через браузер google chrome или mozilla firefox. Если сообщение не исчезает - напишите о проблеме в обратную связь. Спасибо.
Если кнопки скачивания не
загрузились
НАЖМИТЕ ЗДЕСЬ или обновите страницу
Если возникают проблемы со скачиванием, пожалуйста напишите в поддержку по адресу внизу
страницы.
Спасибо за использование сервиса savevideohd.ru
Discover how to modify your Rust code to send `HTTP requests` asynchronously, ensuring that the responses are handled efficiently and effectively. --- This video is based on the question https://stackoverflow.com/q/70772751/ asked by the user 'iustin' ( https://stackoverflow.com/u/14149793/ ) and on the answer https://stackoverflow.com/a/70777909/ provided by the user 'battlmonstr' ( https://stackoverflow.com/u/1009546/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: HTTP request not being sent async Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com. --- Sending HTTP Requests Asynchronously in Rust When building a program that sends multiple HTTP requests, managing concurrency effectively is essential for performance. One common issue many encounter is unintentionally sending requests sequentially instead of asynchronously. In this post, we will investigate a specific example in Rust and learn how to modify the code to achieve true asynchronous behavior for HTTP requests. Understanding the Problem The initial setup involves sending a list of URLs, retrieving their StatusCode and body. However, the code snippet below reveals that the requests are being sent one by one due to a couple of issues in the implementation: [[See Video to Reveal this Text or Code Snippet]] The output indicates that the program seems to wait for each request's complete response before proceeding to the next. This situation can be frustrating, especially when working with numerous URLs. Analyzing the Code The following two points explain why the requests are not being sent asynchronously and how to address the issue: Unintended Blocking with .map() The line: [[See Video to Reveal this Text or Code Snippet]] inadvertently forces the program to complete each future before moving on to the next one. This behavior locks the process into a sequential manner, rendering the buffer_unordered() function ineffective. Request Conversion The requests within filter_map produce a Result, but since those are not categorized as futures directly, this also restricts them from being handled in an asynchronous manner correctly. Solution: Modifying the Code To correct this, the following adjustments are recommended: Step 1: Replace filter_map with map Instead of using filter_map, utilize map to convert URLs into asynchronous tasks (futures): [[See Video to Reveal this Text or Code Snippet]] Step 2: Update the Buffering Technique You should then buffer the futures that are ready to run without blocking: [[See Video to Reveal this Text or Code Snippet]] Step 3: Post-Processing of Errors If there is a need to filter out any errors gracefully, it can be done after buffering: [[See Video to Reveal this Text or Code Snippet]] Conclusion By making these simple changes, you can enhance the performance of Rust applications that are responsible for making multiple HTTP requests. The key takeaway is to ensure that you manage the futures properly and allow them to operate independently rather than locking them into a sequential order. Don’t let poor concurrency practices slow your application down; consider implementing these adjustments in your own code and observe the benefits of true asynchronous processing. Happy coding!