网络编程异步

重点(Top highlight)

Asynchronous programming is not a new concept and has been around for several years and it is nowadays a new standard in modern frameworks like AspNetCore. This framework is now fully asynchronous and it's not easy to avoid using its async keyword.

异步编程并不是一个新概念,它已经存在了好几年,如今它已成为AspNetCore等现代框架中的新标准。 现在,该框架是完全异步的,要避免使用其async关键字并非易事。

I think there is a certain cloud of fear when it comes to asynchronism, developers sometimes feel afraid of using it because they don’t understand how to use it and its best practices.

我认为异步方面存在一定的恐惧感,开发人员有时会害怕使用它,因为他们不了解如何使用它及其最佳实践。

Another reason for this sense of fear is due to the fact that in the past asynchronous programming was not easy at all indeed. Since the introduction of async/await keywords in C# 5 asynchronous programming has become much easier to work with.

这种恐惧感的另一个原因是,在过去,异步编程的确根本不容易。 自从C#5中引入async / await关键字以来,异步编程就变得更加容易使用。

If you are afraid of asynchronism you have come to the right place, there is no need to jump up to me! In this article, I’ll present you with some of the best practices when it comes to async programming in .NET and you’ll see that's not that big of a deal you might think it is.

如果您担心异步问题,那么您来对地方了,您无需跳到我身边! 在本文中,我将向您介绍有关.NET中异步编程的一些最佳实践,并且您会发现这并不是您想的那么重要。

什么是异步编程? (What is Asynchronous Programming?)

“Asynchronous programming is a means of parallel programming in which a unit of work runs separately from the main application thread and notifies the calling thread of its completion, failure or progress.” — Source

“ 异步编程是并行编程的一种方式,其中一个工作单元与主应用程序线程分开运行,并向调用线程通知其完成,失败或进度。” —来源

The core of asynchronous programming are theTask objects, which model asynchronous operations. They are supported by the async and await keywords.

异步编程的核心是Task对象,它们对异步操作进行建模。 它们由asyncawait关键字支持。

The await keyword is where the magic happens. It yields control to the caller of the method that performed await, and it ultimately allows a service to be elastic.

await关键字是神奇的地方。 它将控制权交给执行await的方法的调用者,最终使服务具有弹性。

一路异步 (Async all the way)

When using asynchronous programming you should use it all the way, this means that all your callers should be async. The effort in asynchronous programming can be worthless if you don’t apply it all the way in your code.

当使用异步编程时,应该完全使用它,这意味着所有调用者都应该是异步的。 如果您不完全在代码中应用异步编程,那么付出的努力可能是毫无价值的。

In some cases partially asynchronism can outcome worst results that fully synchronous programming, therefore my advice is to go asynchronous all over your entire stack.

在某些情况下,部分异步可能会导致比完全同步编程更糟糕的结果,因此,我的建议是在整个堆栈中都进行异步处理。

This next piece of code is a good example where sync is preferable over async. Here the use of Task.Result blocks the current thread to wait for the result.

下一段代码是一个很好的示例,其中,同步比异步更可取。 这里使用Task.Result阻止当前线程等待结果。

public int MyMethodAsync(){    var result = OtherMethodAsync().Result;    return result + 1;}

The right way to do this is by using the await keyword to get the result:

正确的方法是使用await关键字来获取结果:

public async Task<int> MyMethodAsync(){    var result = await OtherMethodAsync();    return result + 1;}

只是不要返回异步虚空,相信我 (Just don’t return async void, trust me)

It’s never a good option to use to return void in an async method in .Net, never.

永远不要在异步方法中使用void返回void,这绝对不是一个好选择。

The most common situation where people feel like async void is a good idea is when implementing a fire-and-forget, it isn’t at all. If any exception is thrown in an async void method the process will be stoped.

人们通常认为异步无效是一个好主意,这是在实施“一劳永逸”时,它根本不是。 如果异步void方法中引发了任何异常,则该过程将停止。

So instead of using async void you can use the Task.Run since if any unhandled exception happens the TaskScheduler.UnobservedTaskException will be triggered.

因此,可以使用Task.Run而不是使用异步void,因为如果发生任何未处理的异常,则会触发TaskScheduler.UnobservedTaskException

public void RunFireAndForgetOperation(){    Task.Run(FireAndForgetAsync);}public async Task FireAndForgetAsync(){    var result = await RunOperationAsync();    ParseResult(result);}

异步/等待任务返回 (Async/await over Task return)

Please take this one with a pinch of salt because when it comes to performance, returning directly a Task is faster because there is no computation done in the async state machine but with this, you can end up not taking advantage of the async state machine benefits. The StateMachine is a struct created when using the async/await keywords.

请花一点时间,因为在性能方面,直接返回一个Task会更快,因为异步状态机中没有进行任何计算,但是这样做最终可能无法利用异步状态机的优势。 StateMachine是使用async / await关键字创建的结构。

So there are several benefits when using the async/await keywords:

因此,使用async / await关键字有几个好处:

  • Asynchronous and synchronous exceptions are normalized to always be asynchronous;异步和同步异常被规范化为总是异步的。
  • Exceptions thrown will be automatically wrapped in the returned Task instead of surprising the caller with an actual exception;

    抛出的异常将自动包装在返回的Task而不会使调用者感到实际异常。

  • The code is easier to modify;该代码更易于修改;

To wrap up, these are some practices that you should follow when it comes to asynchronous programming and there are lots of great resources over the internet that you can use to know more about this subject and enhance your skills.

总结起来,这些是异步编程时应遵循的一些做法,并且互联网上有很多很棒的资源,您可以用来了解更多有关该主题的知识并提高技能。

Asynchronous programming can simultaneously be your best ally along with being your worst enemy, the key to success is to study and understand how to use it properly.

异步编程既可以成为最大的敌人,也可以同时成为您的最佳盟友,成功的关键是学习并了解如何正确使用它。

Remember to always look up to understanding how things really work on a deeper level and don’t try to just follow the patterns or you can end up in a Cargo Cult Anti-Pattern.

请记住,始终要了解事物在更深层次上的真实运作方式,不要试图仅仅遵循模式,否则您可能会遇到“货邪教反模式” 。

Nelson out!

尼尔森出去!

翻译自: https://medium.com/@nelsonparente/net-async-programming-in-a-nutshell-dc01c2e71a20

网络编程异步


http://www.taodudu.cc/news/show-5665346.html

相关文章:

  • 暑游。泸沽湖。
  • 跑不过时间就跑过昨天的自己(早安心语)
  • 必备唯美的早安心语语录大集合41条14句
  • android 2.1你好八月,8月你好早安心语
  • 韩泰轮胎:备胎怎么换?三步轻松搞定!
  • 远程控制如何保证安全?浅析向日葵从体系到功能两个方面的安全举措
  • 电工基本安全操作规程
  • 傲游浏览器如何设置缓存文件位置 浏览器缓存文件位置设置方法
  • 知识图谱看高考
  • 用编程给正处于高考考场的小伙伴们加油
  • 计算机高考怎么复习,高考倒计时30天,清北学霸倾情传授高效复习秘籍
  • Python分析42年高考数据,告诉你高考为什么这么难?
  • 十年寒窗,高考加油!
  • 计算机小高考,参加小高考可以选什么专业?参加小高考有什么好处?
  • 【源码+演示】高考加油!HTML+CSS特效文字祝福_程序员祝福高考学子旗开得胜!
  • 高考祝福帖
  • 高考祝福,奔涌吧后浪!送给不平凡的2020高考生
  • 即时成像相机有了新发展 收据纸上印照片
  • 和摄影相关的英文词汇
  • Instant Lab:iPhone变身拍立得
  • 拍立得mini90详解
  • 拍立得
  • 校验 MD5 值
  • Windows系统下对文件进行MD5校验
  • python md5_python中md5
  • Linux文件MD5校验
  • 安全编程之MD5文件校验
  • Ipad重置
  • iPhone和iPad不能充电的解决办法(一定条件下)
  • ipad电量低不能充电_电池电量低时如何为环形门铃充电

网络编程异步_概括地说,网络异步编程相关推荐

  1. python3实用编程技巧_适合Python初学者的一些编程技巧

    这篇文章主要介绍了给Python初学者的一些编程技巧,皆是基于基础的一些编程习惯建议,需要的朋友可以参考下 交换变量 x = 6 y = 5 x, y = y, x print x >>& ...

  2. wedo巡线机器人编程教程_这是一个机器人和编程的时代

    图中在草地上自在奔跑的机器人是波士顿动力公司(BostonDynamics) 开发的类人双足机器人Atlas,由麻省理工(MIT)电子工程与计算机科学系的教授马克·雷波特在1992年创立,一直致力于将 ...

  3. ironbot智能编程机器人_边玩边学,编程启蒙,IronBot机器人套件视频图文评测

    前言 当今世界充满竞争和挑战,你的对手可能不是人.早在2015年,教育部就颁发了指导意见,鼓励以编程为核心的,包括科学(S).技术(T).工程(E).艺术和航空(A),以及数学(M)五个方面的&quo ...

  4. 高并发 python socket send 异步_在Python中使用异步Socket编程性能测试

    ok,首先写一个python socket的server段,对开放三个端口:10000,10001,10002.krondo的例子中是每个server绑定一个端口,测试的时候需要分别开3个shell, ...

  5. java转网络字节序_【转】网络字节序与主机字节序

    最近在项目开发过程中,需要在采用JAVA作为语言的服务器与采用C++作为语言的服务器间进行通信,这就涉及到这两种语言间数据类型的转换以及网络字节序与主机字节序的区别.该文主要说说网络字节序和主机字节序 ...

  6. java 网络抓包_基于java的网络抓包方法

    本实验是用java实现的网络抓包程序,在windows环境下安装winpcap4.0和jpcap6.0后,下载eclipse和jigloo插件(一种在eclipse底下作图形化开发的工具),将其安装好 ...

  7. 基于python的网络爬虫系统_基于Python对网络爬虫系统的设计与实现.pdf

    基于Python对网络爬虫系统的设计与实现.pdf 日期: 2020-08-02 01:17:51 人气: - 基于Python对网络爬虫系统的设计与实现软件研发与应用SOFTWARE DEVELOP ...

  8. 网络研讨室_即将举行的网络研讨会:调试生产中Java的5种最佳实践

    网络研讨室 您的团队是否花费超过10%的时间在生产中调试Java? 将新代码部署到生产中是一项艰巨的任务. 在您的本地环境中起作用的东西在生产中的作用并不相同,您可以通过用户来了解. 不理想吧? 生产 ...

  9. 网络语言嗨C,网络流行语大全_最新最热门网络语言

    大步跨入2016年,掐指一算,惊恐地发现90后孩子们都到了晚婚晚育年龄了!作为80后.90后,看看你们还能和00后"无缝对接"吗?2016最新网络语言你知道多少? 吃藕 单看&qu ...

最新文章

  1. kali安装vscode和无法启动解决方法
  2. 犹太人的思维习惯 (转载)
  3. 解决opacity属性在低版本IE浏览器下失效的方法
  4. 十进制数与八进制数互相转换(MATLAB和C版本)
  5. 次坐标从0开始_干货 | 全站仪测量使用方法及坐标计算,一步一步讲解!
  6. theano安装教程 linux,Ubuntu安装Theano+CUDA
  7. 国际信用卡VISA/MasterCard/AE/DC/JCB 卡号结构
  8. 无源贴片晶振四角引脚_有源晶振四引脚详细用法
  9. Centos 7搭建PPTP服务器方法
  10. 利用python解决简单数独
  11. ENVI下Landsat8大气校正法反演地表温度
  12. 地面气象观测数据-A文件转excel方法
  13. 2021双十一自动浏览脚本,京东淘宝自动化脚本末班车
  14. python数据分析与可视化
  15. C++基础知识(上)
  16. 蓝牙开发那些事(9)——结合代码看a2dp协议
  17. 谈谈半个月 Goolge Baidu Live yahoo收录情况。
  18. 台风康妮超级计算机,台风康妮最新消息 2013年第15号台风康妮实时路径图
  19. 绝密!!!!!明星的身份证照片!!!!
  20. 转炉炼钢计算机仿真实验报告,计算机仿真冶炼运用(共4523字).doc

热门文章

  1. 第十二章 存储类别、链接和内存管理
  2. matlab md5.dll,md5.dll下载|
  3. html轮播底部圆点怎么做,图片轮播右下角的小圆点怎么加上去?
  4. 粉丝破万啦!!感谢,感恩
  5. 禁止ThinkVantage Password Manager自启动
  6. 无尽神域服务器维护,无尽神域突然显示数据包损坏怎么办 解决方案一览
  7. Java学习篇五——分支(选择)结构之switch语句及其中的break作用
  8. 玩:你玩过下面哪些儿时的玩具?
  9. php 上传图片 ftp,ftp文件上传_php ftp文件上传
  10. python --图片合成视频