Saturday , February 27 2021

crazyguitar / pysheeet, Hacker News

    

Abstract

The C Is still a puzzle for a programmer to find a way to solve it. Generally, developers deal with extensive I / O operations via

thread ,

epoll , or kqueue to avoid their software waiting for an expensive task. However, developing a readable and bug-free concurrent code is challenging due to data sharing and job dependency. Even though some powerful tools, such as Valgrind , help developers to detect deadlock or other asynchronous issues, solving these problems may be time-consuming when the scale of software grows large. Therefore, many programming languages ​​such as Python, Javascript, or C dedicated to developing better libraries, frameworks, or syntaxes to assist programmers in managing concurrent jobs properly. Instead of focusing on how to use modern parallel APIs, this article mainly concentrates on the design philosophy behind asynchronous programming patterns.

Using threads is a more natural way for developers to dispatch tasks without blocking the main thread. However, threads may lead to performance issues such as locking critical sections to do some atomic operations. Although using event-loop can enhance performance in some cases, writing readable code is challenging due to callback problems (e.g., callback hell). Fortunately, programming languages ​​like Python introduced a concept, async / await , to help developers write readable code with high performance. The following figure shows the main goal to use async / await to handle socket connections like utilizing threads.

Introduction

Handling I / O operations such as network connections is one of the most expensive tasks in a program. Take a simple TCP blocking echo server as an example (The following snippet). If a client connects to the server successfully without sending any request, it blocks others' connections. Even though clients send data As soon as possible, the server cannot handle other requests if there is no client tries to establish a connection. Also, handling multiple requests is inefficient because it wastes a lot of time waiting for I / O responses from hardware such as network interfaces. Thus, socket programming with concurrency becomes inevitable to manage extensive requests.

(import) socket

   s 

=

  (socket) .  socket 

(import) socket

  
  (selectors) (import)  
 DefaultSelector  , 
EVENT_READ
,  EVENT_WRITE  
  (functools) (import)  
 partial  s 

=

  (socket) .  socket 

About admin

Leave a Reply

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