简体   繁体   中英

More than one process at the same time

Hey I am learning Python at the moment. I wrote a few programs. Now I have a question: Is it possible to run more "operations" at once? According to my knowledge the scripts runs from the top to the bottom (except from thing like called def and if statements and so on).


For example: I want to do something and wait 5 seconds an then continue but while my program "waits" it should do something other? (This one is very simple) Or: While checking for input do something other output things.

The examples are very poor but I do not finde something better at the moment. (If something comes to my mind, I will add it later) I hope you understand what my question is.

TL;DR: Use an async approach. Raymond Hettinger is a god, and this talk explains this concept more accurately and thoroughly than I can. ;)

The behavior you are describing is called "concurrency" or "asynchronicity", where you have more than one "piece" of code executing "at the same time". This is one of the hardest problems in practical computer science, because adding the dimension of time causes scheduling problems in addition to logic problems. However, it is very much in demand these days because of multi-core processors and the inherently parallel environment of the internet

"At the same time" is in quotes, because there are two basic ways to make this happen:

  1. actually run the code at the same time
  2. make it look like it is running at the same time.

The first option is called Concurrent programing, and the second is called Asynchronous programming (commonly "async").

Generally, "modern" programming seems to favor async, because it's easier to reason about and comes with fewer, less severe pitfalls. If you do it right, async programs can look a lot like the synchronous, procedural code you're already familiar with. Golang is basically built on the concept. Javascript has embraced "futures" in the form of Promises and async / await . I know it's not Python, but this talk by the creator of Go gives a good overview of the philosophy.

Python gives you three main ways to approach this, separated into three major modules: threading , multiprocessing , and asyncio

multiprocessing and threading are concurrent solutions. They do very similar things, but accomplish them in slightly different ways by delegating to the OS in different ways. This answer has a concise explanation of the difference. Concurrency is notoriously difficult to debug, because it is not deterministic : small differences in timing can result in completely different sequences of execution. You also have to deal with "race conditions" in threads, where two bits of code want to read/change the same piece of shared state at the same time.

asyncio , or "asynchronous input-output" is a more recent, async solution. You'll need at least Python 3.4. It uses event loops to allow long-running tasks to execute without "blocking" the rest of the program. Processes and threads do a similar thing, running two or more operations on even the same processor core by interrupting the running process periodically, forcing them to take turns. But with async, you decide where the turn-taking happens. It's like designing mature adults that interact cooperatively rather than designing kindergarteners that have to be baby-sat by the OS and forced to share the processor.

There are also third-party packages like gevent and eventlet that predate asyncio and work in earlier versions of Python. If you can afford to target Python >=3.4, I would recommend just using asyncio , because it's part of the Python core.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM