{"id":359940,"date":"2024-05-21T00:42:57","date_gmt":"2024-05-21T00:42:57","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=359940"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=359940","title":{"rendered":"<span>\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0433\u0430\u0439\u0434 \u043f\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c \u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c (\u0438 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e) \u0432 Python<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0417\u0430 \u0432\u0440\u0435\u043c\u044f \u043c\u0435\u043d\u0442\u043e\u0440\u0441\u0442\u0432\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0437\u0430\u043c\u0435\u0442\u0438\u043b, \u0447\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432  \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0441 \u0442\u0435\u043c\u0430\u043c\u0438: \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c, \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c, \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u044c. \u0418\u0445 \u0447\u0430\u0441\u0442\u043e \u0437\u0430\u0434\u0430\u044e\u0442 \u043d\u0430 \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f\u0445, \u0430 \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 \u044d\u0442\u0438 \u0437\u043d\u0430\u043d\u0438\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043f\u0438\u0441\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0435 \u0432\u0438\u0434\u0435\u043d\u0438\u0435 \u0438 \u043e\u043f\u044b\u0442 \u0432 \u0432\u0438\u0434\u0435 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<p>\u0426\u0435\u043b\u044c &#8212; c \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c:<\/p>\n<ul>\n<li>\n<p>\u041e \u043f\u043e\u0442\u043e\u043a\u0430\u0445, \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u0445 \u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445.<\/p>\n<\/li>\n<li>\n<p>\u041e Global Interpreter Lock \u0438 \u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c\u044b\u0445 \u0438\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u0445.<\/p>\n<\/li>\n<li>\n<p>CPU \u0438 I\/O bound \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0447\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u0430 \u043e\u0442 ThreadPool \u0438 ProcessPool.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430 \u21161: \u0420\u0430\u0431\u043e\u0442\u0430 \u0441 JSON \u0438\u043b\u0438 CPU-bound task<\/h2>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e: <\/p>\n<pre><code class=\"python\"># CPU bound task: generate list and dump it into JSON def work(size):     json.dumps(list(range(size)))<\/code><\/pre>\n<p>\u041e\u043d\u0430 \u0434\u0435\u043b\u0430\u0435\u0442 2 \u0432\u0435\u0449\u0438: \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0438 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0433\u043e \u0432 JSON. \u041f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043a\u043e\u0434 &#8212; \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 CPU bound \u0437\u0430\u0434\u0430\u0447\u0438 &#8212;  \u0437\u0430\u0434\u0430\u0447\u0438, \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043c\u043e\u0449\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u044d\u0442\u0443 \u0442\u044f\u0436\u0435\u043b\u043e\u0432\u0435\u0441\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 N \u0440\u0430\u0437. \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\"># Do work sequentially, one by one def sequential(size, count):     for _ in range(count):         work(size)<\/code><\/pre>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e. \u041a\u0430\u043a\u0438\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u043d\u0430\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b?<\/p>\n<p>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u21161 &#8212; \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 <strong>\u043f\u043e\u0442\u043e\u043a\u0443<\/strong> \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430:<\/p>\n<pre><code class=\"python\"># Do work using OS Threads def run_threads(size, executionUnitsCount):     with concurrent.futures.ThreadPoolExecutor() as executor:         executor.map(work, [size] * executionUnitsCount)<\/code><\/pre>\n<p>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u21162 &#8212; \u0432\u043c\u0435\u0441\u0442\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c <strong>\u043f\u0440\u043e\u0446\u0435\u0441\u0441<\/strong>:<\/p>\n<pre><code class=\"python\"># Do work using OS Processes def run_processes(size, executionUnitsCount):     with concurrent.futures.ProcessPoolExecutor() as executor:         executor.map(work, [size] * executionUnitsCount)<\/code><\/pre>\n<p>\u0412 \u043e\u0431\u043e\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f executor \u043a\u043b\u0430\u0441\u0441\u044b \u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 <code>\u0441oncurrent.futures<\/code>. \u0415\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043a\u0435\u0442\u044b <code>multithreading<\/code> \u0438 <code>multiprocessing<\/code> \u0442\u043e \u043a\u043e\u0434 \u0431\u044b \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>Hidden text<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\"># Do work using OS Threads def run_threads(size, executionUnitsCount):     threads = [threading.Thread(target=work, args=(size,)) for _ in range(executionUnitsCount)]      for t in threads:         t.start()          for t in threads:         t.join()  # Do work using OS Processes def run_processes(size, executionUnitsCount):     processes = [multiprocessing.Process(target=work, args=(size,)) for _ in range(executionUnitsCount)]          for p in processes:         p.start()          for p in processes:         p.join()<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041a\u043e\u0434 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0441\u0435\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439:<\/p>\n<pre><code class=\"python\">if __name__ == '__main__':     # Disable GC for better benchmarking and avoid pauses     gc.disable()          jsonSize = 1000000     testCases = [ (jsonSize,  i) for i in range(1, 11)]     variants = [sequential, run_threads, run_processes]      for i, t in enumerate(testCases):         size, executionUnitsCount = t         print(f\"Parallelism: {executionUnitsCount}, JSON Size: {size}\")                  for j, variant in enumerate(variants):                     start = time.perf_counter()              variant(size, executionUnitsCount)              end = time.perf_counter()              print(f\"{variant.__name__}, elapsed: {round(end - start, 2)}\")         print()<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<figure class=\"bordered full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d5c\/e3a\/ea5\/d5ce3aea58f101ada114d61478b3cfa2.png\" alt=\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430. \u041e\u0441\u044c X - \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447. \u041e\u0441\u044c Y - \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445\" title=\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430. \u041e\u0441\u044c X - \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447. \u041e\u0441\u044c Y - \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445\" width=\"1134\" height=\"666\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d5c\/e3a\/ea5\/d5ce3aea58f101ada114d61478b3cfa2.png\"\/><\/p>\n<div><figcaption>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430. \u041e\u0441\u044c X &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447. \u041e\u0441\u044c Y &#8212; \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445<\/figcaption><\/div>\n<\/figure>\n<p>\u041a\u0430\u043a\u0438\u0435 \u0432\u044b\u0432\u043e\u0434\u044b \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c? <\/p>\n<ul>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u043b\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f CPU bound \u0437\u0430\u0434\u0430\u0447 \u043d\u0435 \u0434\u0430\u0435\u0442 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043c\u044b \u0441 \u043a\u043e\u0434\u043e\u043c  \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u0438 \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e. \u0421 \u0447\u0435\u043c \u044d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e? \u0412\u0441\u0435 \u0434\u0435\u043b\u043e \u0432 GIL &#8212; \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0435 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 &#171;\u043d\u0430\u0431\u0440\u0430\u0442\u044c \u043c\u043e\u0449\u043d\u043e\u0441\u0442\u044c&#187; \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e. <\/p>\n<\/li>\n<li>\n<p>\u041d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b GIL \u043d\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0438 \u0441\u043e\u0437\u0434\u0430\u0432 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u043c\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0443\u0442\u0438\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430 \u043f\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c\u0443 \u0438 \u043a\u0430\u043a \u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u0432\u0438\u0434\u0438\u043c \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435.<\/p>\n<\/li>\n<li>\n<p>\u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f c \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0440\u0430\u0432\u0435\u043d \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u044f\u0434\u0435\u0440 CPU. \u041d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u0447\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430 Processes \u0440\u0430\u0441\u0442\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043b\u0435\u0441\u0435\u043d\u043a\u043e\u0439 \u0441 \u0448\u0430\u0433\u043e\u043c 4 &#8212; \u044d\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u044f\u0434\u0435\u0440 CPU \u043d\u0430 \u043c\u043e\u0435\u043c \u043d\u043e\u0443\u0442\u0431\u0443\u043a\u0435.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430 \u21162: \u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0432\u043d\u0435\u0448\u043d\u0438\u043c API \u0438\u043b\u0438 IO-bound task<\/h2>\n<p>\u041f\u043e\u043c\u0438\u043c\u043e CPU bound \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 IO bound. \u0412 \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043d\u0435 \u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430 \u0430 \u043e\u0442 \u043f\u043e\u0434\u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0432\u0432\u043e\u0434\u0430-\u0432\u044b\u0432\u043e\u0434\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u044b \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0444\u0430\u0439\u043b\u043e\u0432\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0438\u043b\u0438 \u0441\u0435\u0442\u044c.<\/p>\n<p>\u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0438\u0436\u0435 \u044f \u0431\u0443\u0434\u0443 \u044d\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c IO \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0447\u0435\u0440\u0435\u0437 sleep (\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0447\u0442\u043e \u0432\u043c\u0435\u0441\u0442\u043e sleep &#8212; \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e HTTP \u043a API \u0432\u0430\u0448\u0435\u0433\u043e \u043b\u044e\u0431\u0438\u043c\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430).<\/p>\n<pre><code class=\"python\">def work(latency):     time.sleep(latency)<\/code><\/pre>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c N \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u0441\u0435\u0440\u0432\u0438\u0441 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440 \u0443\u0441\u043b\u0443\u0433 \u0438 \u0445\u043e\u0434\u0438\u043c \u043a \u043f\u0430\u0440\u0442\u043d\u0435\u0440\u0430\u043c \u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u043c\u0438). \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">def sequential(latency, count):     for _ in range(count):         work(latency)<\/code><\/pre>\n<p>\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043f\u043e\u0442\u043e\u043a\u0430\u0445:<\/p>\n<pre><code class=\"python\"># Do work using OS Threads def run_threads(latency, executionUnitsCount):     with concurrent.futures.ThreadPoolExecutor(max_workers=executionUnitsCount) as executor:         executor.map(work, [latency] * executionUnitsCount)<\/code><\/pre>\n<p>\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u0445:<\/p>\n<pre><code class=\"python\"># Do work using OS Processes def run_processes(latency, executionUnitsCount):     with concurrent.futures.ProcessPoolExecutor() as executor:         executor.map(work, [latency] * executionUnitsCount)<\/code><\/pre>\n<p>\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 asyncio:<\/p>\n<pre><code class=\"python\">def run_async_io(latency, executionUnitsCount):     asyncio.run(async_io_tasks(latency, executionUnitsCount))   async def async_work(latency):     await asyncio.sleep(latency)   async def async_io_tasks(latency, executionUnitsCount):     tasks = [asyncio.create_task(async_work(latency)) for _ in range(executionUnitsCount)]          await asyncio.gather(*tasks)<\/code><\/pre>\n<p>\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a (\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0434\u0445\u043e\u0434\u0430\u043c\u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u043e\u0442 1000 \u0434\u043e 2000 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447):<\/p>\n<pre><code class=\"python\">if __name__ == '__main__':     testCases = [ (0.1,  i) for i in range(1000, 20001, 200)]     variants = [run_threads, run_async_io, run_processes]          for i, t in enumerate(testCases):         latency, executionUnitsCount = t         print(f\"Parallelism\\: {executionUnitsCount}\")                  for j, variant in enumerate(variants):             start = time.perf_counter()              r = variant(latency, executionUnitsCount)              end = time.perf_counter()             print(f\"{case.__name__}, elapsed: {round(end - start, 2)}\")         print()<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<figure class=\"bordered full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/439\/d97\/72a\/439d9772a0e47f6d44774755f54855ad.png\" alt=\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430. \u041e\u0441\u044c X - \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 (\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432). \u041e\u0441\u044c Y - \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445\" title=\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430. \u041e\u0441\u044c X - \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 (\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432). \u041e\u0441\u044c Y - \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445\" width=\"1128\" height=\"638\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/439\/d97\/72a\/439d9772a0e47f6d44774755f54855ad.png\"\/><\/p>\n<div><figcaption>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430. \u041e\u0441\u044c X &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 (\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432). \u041e\u0441\u044c Y &#8212; \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445<\/figcaption><\/div>\n<\/figure>\n<p>\u0423\u0432\u0435\u043b\u0438\u0447\u0438\u043c \u043c\u0430\u0441\u0448\u0442\u0430\u0431 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u0438 \u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b:<\/p>\n<figure class=\"bordered full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/833\/665\/6d5\/8336656d5d844fa00d4b5725edca04b6.png\" alt=\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430. \u041e\u0441\u044c X - \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 (\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432). \u041e\u0441\u044c Y - \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445\" title=\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430. \u041e\u0441\u044c X - \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 (\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432). \u041e\u0441\u044c Y - \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445\" width=\"1148\" height=\"658\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/833\/665\/6d5\/8336656d5d844fa00d4b5725edca04b6.png\"\/><\/p>\n<div><figcaption>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430. \u041e\u0441\u044c X &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 (\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432). \u041e\u0441\u044c Y &#8212; \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445<\/figcaption><\/div>\n<\/figure>\n<p>\u041a\u0430\u043a\u0438\u0435 \u0432\u044b\u0432\u043e\u0434\u044b \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c? <\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u0432 \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435 \u043d\u0435\u043f\u0440\u0438\u0433\u043e\u0434\u043d\u044b \u0434\u043b\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0437\u0430\u0434\u0430\u0447, \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043e\u0432\u0435\u0440\u0445\u0435\u0434 \u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438 \u0435\u0433\u043e \u0443\u0431\u0438\u0439\u0441\u0442\u0432\u043e, \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u041e\u0421 \u0442\u0430\u043a \u0447\u0430\u0441\u0442\u043e &#8212; \u0434\u043e\u0440\u043e\u0433\u043e\u0435 \u0443\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0442\u043e\u043a\u0438 \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0438\u0441\u044c \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0443\u0447\u0448\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432, \u0438 GIL \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043d\u0435 \u0441\u0442\u0430\u043b \u043f\u043e\u043c\u0435\u0445\u043e\u0439, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u0437\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u0442 &#171;\u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f&#187; \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 IO \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u043e\u0442\u043e\u043a\u0438 \u0434\u043e\u043b\u044c\u0448\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0438 \u0447\u0435\u043c \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 . \u041d\u043e \u043e\u0432\u0435\u0440\u0445\u0435\u0434 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u043c \u0441 \u041e\u0421 \u0438 \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c.<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0440\u0443\u0442\u0438\u043d\u044b &#8212; \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 \u043b\u0438\u0434\u0435\u0440, \u0437\u0430 \u0441\u0447\u0435\u0442 \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0438\u043c\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0440\u0430\u043d\u0442\u0430\u0439\u043c \u044f\u0437\u044b\u043a\u0430 \u0430 \u043d\u0435 \u041e\u0421, \u043a \u0442\u043e\u043c\u0443 \u0436\u0435 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0435\u0435 \u043c\u0435\u043d\u044c\u0448\u0435 \u0447\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u0443.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0415\u0441\u0442\u044c \u043b\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432\/\u043f\u043e\u0442\u043e\u043a\u043e\u0432\/\u043a\u043e\u0440\u0443\u0442\u0438\u043d?<\/h2>\n<p>\u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0432\u044b\u0448\u0435 \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u043b\u0438 \u0440\u0430\u0437\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447 \u0438 \u0443 \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u043e\u0433\u043e \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044f \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441: \u0421\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435? \u0427\u0435\u043c \u043c\u044b \u043f\u043b\u0430\u0442\u0438\u043c \u0437\u0430 \u0438\u0445 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435? \u041e\u0442\u0432\u0435\u0442 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0437\u0430\u0438\u0447\u0435\u043d: \u043c\u044b \u043f\u043b\u0430\u0442\u0438\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043d\u0430\u0448\u0435\u0439 \u043c\u0430\u0448\u0438\u043d\u044b. \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438 \u043f\u043e\u0442\u043e\u043a\u0430 \u041e\u0421 \u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u0443\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u043c\u044f\u0442\u0438, \u0430 \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0430\u043c\u044f\u0442\u044c \u043a\u043e\u043d\u0435\u0447\u043d\u0430 \u0442\u043e \u0431\u0435\u0437 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438\u043b\u0438 \u043f\u043e\u0442\u043e\u043a \u043c\u044b \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u0441\u0442\u043e\u043b\u043a\u043d\u0435\u043c\u0441\u044f \u0441 OOM (Out of memory error).<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0433\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u043e\u0448\u0438\u0431\u043e\u043a \u0432 production \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u0432 ThreadPool \u0438 ProcessPool.<\/p>\n<h2>\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d. \u041d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c \u0432 \u043a\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0442\u043e\u0442 \u0438\u043b\u0438 \u0438\u043d\u043e\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442. \u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u0434 \u0431\u044b\u043b\u0438 \u0432\u044b\u0431\u0440\u0430\u043d\u044b \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0441\u0438\u043d\u0442\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0430\u0449\u0438\u0435 production \u043a\u043e\u0434\u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u043c \u0435\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u043e \u0432 \u0440\u0430\u0431\u043e\u0442\u0435.<\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 <a href=\"https:\/\/github.com\/abstractart\/python-multiprocessing-example\" rel=\"noopener noreferrer nofollow\">GitHub<\/a>.<\/p>\n<p>\u0421\u0442\u0430\u0442\u044c\u0438 \u0434\u043b\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u043b\u0435\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/concurrent.futures.html\" rel=\"noopener noreferrer nofollow\">https:\/\/docs.python.org\/3\/library\/concurrent.futures.html<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/threading.html\" rel=\"noopener noreferrer nofollow\">https:\/\/docs.python.org\/3\/library\/threading.html<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.html\" rel=\"noopener noreferrer nofollow\">https:\/\/docs.python.org\/3\/library\/multiprocessing.html<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/superfastpython.com\/asyncio-vs-threading\/\" rel=\"noopener noreferrer nofollow\">https:\/\/superfastpython.com\/asyncio-vs-threading\/<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0447\u0442\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b\u0438 \u0434\u043e \u043a\u043e\u043d\u0446\u0430, \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u043b\u044e\u0431\u043e\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445!<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/773376\/\"> https:\/\/habr.com\/ru\/articles\/773376\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0417\u0430 \u0432\u0440\u0435\u043c\u044f \u043c\u0435\u043d\u0442\u043e\u0440\u0441\u0442\u0432\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0437\u0430\u043c\u0435\u0442\u0438\u043b, \u0447\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432  \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0441 \u0442\u0435\u043c\u0430\u043c\u0438: \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c, \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c, \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u044c. \u0418\u0445 \u0447\u0430\u0441\u0442\u043e \u0437\u0430\u0434\u0430\u044e\u0442 \u043d\u0430 \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f\u0445, \u0430 \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 \u044d\u0442\u0438 \u0437\u043d\u0430\u043d\u0438\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043f\u0438\u0441\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0435 \u0432\u0438\u0434\u0435\u043d\u0438\u0435 \u0438 \u043e\u043f\u044b\u0442 \u0432 \u0432\u0438\u0434\u0435 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<p>\u0426\u0435\u043b\u044c &#8212; c \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c:<\/p>\n<ul>\n<li>\n<p>\u041e \u043f\u043e\u0442\u043e\u043a\u0430\u0445, \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u0445 \u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445.<\/p>\n<\/li>\n<li>\n<p>\u041e Global Interpreter Lock \u0438 \u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c\u044b\u0445 \u0438\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u0445.<\/p>\n<\/li>\n<li>\n<p>CPU \u0438 I\/O bound \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0447\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u0430 \u043e\u0442 ThreadPool \u0438 ProcessPool.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430 \u21161: \u0420\u0430\u0431\u043e\u0442\u0430 \u0441 JSON \u0438\u043b\u0438 CPU-bound task<\/h2>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e: <\/p>\n<pre><code class=\"python\"># CPU bound task: generate list and dump it into JSON def work(size):     json.dumps(list(range(size)))<\/code><\/pre>\n<p>\u041e\u043d\u0430 \u0434\u0435\u043b\u0430\u0435\u0442 2 \u0432\u0435\u0449\u0438: \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0438 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0433\u043e \u0432 JSON. \u041f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043a\u043e\u0434 &#8212; \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 CPU bound \u0437\u0430\u0434\u0430\u0447\u0438 &#8212;  \u0437\u0430\u0434\u0430\u0447\u0438, \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043c\u043e\u0449\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u044d\u0442\u0443 \u0442\u044f\u0436\u0435\u043b\u043e\u0432\u0435\u0441\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 N \u0440\u0430\u0437. \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\"># Do work sequentially, one by one def sequential(size, count):     for _ in range(count):         work(size)<\/code><\/pre>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e. \u041a\u0430\u043a\u0438\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u043d\u0430\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b?<\/p>\n<p>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u21161 &#8212; \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 <strong>\u043f\u043e\u0442\u043e\u043a\u0443<\/strong> \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430:<\/p>\n<pre><code class=\"python\"># Do work using OS Threads def run_threads(size, executionUnitsCount):     with concurrent.futures.ThreadPoolExecutor() as executor:         executor.map(work, [size] * executionUnitsCount)<\/code><\/pre>\n<p>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u21162 &#8212; \u0432\u043c\u0435\u0441\u0442\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c <strong>\u043f\u0440\u043e\u0446\u0435\u0441\u0441<\/strong>:<\/p>\n<pre><code class=\"python\"># Do work using OS Processes def run_processes(size, executionUnitsCount):     with concurrent.futures.ProcessPoolExecutor() as executor:         executor.map(work, [size] * executionUnitsCount)<\/code><\/pre>\n<p>\u0412 \u043e\u0431\u043e\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f executor \u043a\u043b\u0430\u0441\u0441\u044b \u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 <code>\u0441oncurrent.futures<\/code>. \u0415\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043a\u0435\u0442\u044b <code>multithreading<\/code> \u0438 <code>multiprocessing<\/code> \u0442\u043e \u043a\u043e\u0434 \u0431\u044b \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>Hidden text<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\"># Do work using OS Threads def run_threads(size, executionUnitsCount):     threads = [threading.Thread(target=work, args=(size,)) for _ in range(executionUnitsCount)]      for t in threads:         t.start()          for t in threads:         t.join()  # Do work using OS Processes def run_processes(size, executionUnitsCount):     processes = [multiprocessing.Process(target=work, args=(size,)) for _ in range(executionUnitsCount)]          for p in processes:         p.start()          for p in processes:         p.join()<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041a\u043e\u0434 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0441\u0435\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439:<\/p>\n<pre><code class=\"python\">if __name__ == '__main__':     # Disable GC for better benchmarking and avoid pauses     gc.disable()          jsonSize = 1000000     testCases = [ (jsonSize,  i) for i in range(1, 11)]     variants = [sequential, run_threads, run_processes]      for i, t in enumerate(testCases):         size, executionUnitsCount = t         print(f\"Parallelism: {executionUnitsCount}, JSON Size: {size}\")                  for j, variant in enumerate(variants):                     start = time.perf_counter()              variant(size, executionUnitsCount)              end = time.perf_counter()              print(f\"{variant.__name__}, elapsed: {round(end - start, 2)}\")         print()<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<figure class=\"bordered full-width\">\n<div><figcaption>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430. \u041e\u0441\u044c X &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447. \u041e\u0441\u044c Y &#8212; \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445<\/figcaption><\/div>\n<\/figure>\n<p>\u041a\u0430\u043a\u0438\u0435 \u0432\u044b\u0432\u043e\u0434\u044b \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c? <\/p>\n<ul>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u043b\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f CPU bound \u0437\u0430\u0434\u0430\u0447 \u043d\u0435 \u0434\u0430\u0435\u0442 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043c\u044b \u0441 \u043a\u043e\u0434\u043e\u043c  \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u0438 \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e. \u0421 \u0447\u0435\u043c \u044d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e? \u0412\u0441\u0435 \u0434\u0435\u043b\u043e \u0432 GIL &#8212; \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0435 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 &#171;\u043d\u0430\u0431\u0440\u0430\u0442\u044c \u043c\u043e\u0449\u043d\u043e\u0441\u0442\u044c&#187; \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e. <\/p>\n<\/li>\n<li>\n<p>\u041d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b GIL \u043d\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0438 \u0441\u043e\u0437\u0434\u0430\u0432 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u043c\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0443\u0442\u0438\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430 \u043f\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c\u0443 \u0438 \u043a\u0430\u043a \u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u0432\u0438\u0434\u0438\u043c \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435.<\/p>\n<\/li>\n<li>\n<p>\u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f c \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0440\u0430\u0432\u0435\u043d \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u044f\u0434\u0435\u0440 CPU. \u041d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u0447\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430 Processes \u0440\u0430\u0441\u0442\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043b\u0435\u0441\u0435\u043d\u043a\u043e\u0439 \u0441 \u0448\u0430\u0433\u043e\u043c 4 &#8212; \u044d\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u044f\u0434\u0435\u0440 CPU \u043d\u0430 \u043c\u043e\u0435\u043c \u043d\u043e\u0443\u0442\u0431\u0443\u043a\u0435.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430 \u21162: \u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0432\u043d\u0435\u0448\u043d\u0438\u043c API \u0438\u043b\u0438 IO-bound task<\/h2>\n<p>\u041f\u043e\u043c\u0438\u043c\u043e CPU bound \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 IO bound. \u0412 \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043d\u0435 \u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430 \u0430 \u043e\u0442 \u043f\u043e\u0434\u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0432\u0432\u043e\u0434\u0430-\u0432\u044b\u0432\u043e\u0434\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u044b \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0444\u0430\u0439\u043b\u043e\u0432\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0438\u043b\u0438 \u0441\u0435\u0442\u044c.<\/p>\n<p>\u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0438\u0436\u0435 \u044f \u0431\u0443\u0434\u0443 \u044d\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c IO \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0447\u0435\u0440\u0435\u0437 sleep (\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0447\u0442\u043e \u0432\u043c\u0435\u0441\u0442\u043e sleep &#8212; \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e HTTP \u043a API \u0432\u0430\u0448\u0435\u0433\u043e \u043b\u044e\u0431\u0438\u043c\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430).<\/p>\n<pre><code class=\"python\">def work(latency):     time.sleep(latency)<\/code><\/pre>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c N \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u0441\u0435\u0440\u0432\u0438\u0441 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440 \u0443\u0441\u043b\u0443\u0433 \u0438 \u0445\u043e\u0434\u0438\u043c \u043a \u043f\u0430\u0440\u0442\u043d\u0435\u0440\u0430\u043c \u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u043c\u0438). \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">def sequential(latency, count):     for _ in range(count):         work(latency)<\/code><\/pre>\n<p>\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043f\u043e\u0442\u043e\u043a\u0430\u0445:<\/p>\n<pre><code class=\"python\"># Do work using OS Threads def run_threads(latency, executionUnitsCount):     with concurrent.futures.ThreadPoolExecutor(max_workers=executionUnitsCount) as executor:         executor.map(work, [latency] * executionUnitsCount)<\/code><\/pre>\n<p>\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u0445:<\/p>\n<pre><code class=\"python\"># Do work using OS Processes def run_processes(latency, executionUnitsCount):     with concurrent.futures.ProcessPoolExecutor() as executor:         executor.map(work, [latency] * executionUnitsCount)<\/code><\/pre>\n<p>\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 asyncio:<\/p>\n<pre><code class=\"python\">def run_async_io(latency, executionUnitsCount):     asyncio.run(async_io_tasks(latency, executionUnitsCount))   async def async_work(latency):     await asyncio.sleep(latency)   async def async_io_tasks(latency, executionUnitsCount):     tasks = [asyncio.create_task(async_work(latency)) for _ in range(executionUnitsCount)]          await asyncio.gather(*tasks)<\/code><\/pre>\n<p>\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a (\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0434\u0445\u043e\u0434\u0430\u043c\u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u043e\u0442 1000 \u0434\u043e 2000 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447):<\/p>\n<pre><code class=\"python\">if __name__ == '__main__':     testCases = [ (0.1,  i) for i in range(1000, 20001, 200)]     variants = [run_threads, run_async_io, run_processes]          for i, t in enumerate(testCases):         latency, executionUnitsCount = t         print(f\"Parallelism\\: {executionUnitsCount}\")                  for j, variant in enumerate(variants):             start = time.perf_counter()              r = variant(latency, executionUnitsCount)              end = time.perf_counter()             print(f\"{case.__name__}, elapsed: {round(end - start, 2)}\")         print()<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<figure class=\"bordered full-width\">\n<div><figcaption>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430. \u041e\u0441\u044c X &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 (\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432). \u041e\u0441\u044c Y &#8212; \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445<\/figcaption><\/div>\n<\/figure>\n<p>\u0423\u0432\u0435\u043b\u0438\u0447\u0438\u043c \u043c\u0430\u0441\u0448\u0442\u0430\u0431 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u0438 \u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b:<\/p>\n<figure class=\"bordered full-width\">\n<div><figcaption>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0430. \u041e\u0441\u044c X &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 (\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432). \u041e\u0441\u044c Y &#8212; \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445<\/figcaption><\/div>\n<\/figure>\n<p>\u041a\u0430\u043a\u0438\u0435 \u0432\u044b\u0432\u043e\u0434\u044b \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c? <\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u0432 \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435 \u043d\u0435\u043f\u0440\u0438\u0433\u043e\u0434\u043d\u044b \u0434\u043b\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0437\u0430\u0434\u0430\u0447, \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043e\u0432\u0435\u0440\u0445\u0435\u0434 \u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438 \u0435\u0433\u043e \u0443\u0431\u0438\u0439\u0441\u0442\u0432\u043e, \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u041e\u0421 \u0442\u0430\u043a \u0447\u0430\u0441\u0442\u043e &#8212; \u0434\u043e\u0440\u043e\u0433\u043e\u0435 \u0443\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0442\u043e\u043a\u0438 \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0438\u0441\u044c \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0443\u0447\u0448\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432, \u0438 GIL \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043d\u0435 \u0441\u0442\u0430\u043b \u043f\u043e\u043c\u0435\u0445\u043e\u0439, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u0437\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u0442 &#171;\u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f&#187; \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 IO \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u043e\u0442\u043e\u043a\u0438 \u0434\u043e\u043b\u044c\u0448\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0438 \u0447\u0435\u043c \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 . \u041d\u043e \u043e\u0432\u0435\u0440\u0445\u0435\u0434 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u043c \u0441 \u041e\u0421 \u0438 \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c.<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0440\u0443\u0442\u0438\u043d\u044b &#8212; \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 \u043b\u0438\u0434\u0435\u0440, \u0437\u0430 \u0441\u0447\u0435\u0442 \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0438\u043c\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0440\u0430\u043d\u0442\u0430\u0439\u043c \u044f\u0437\u044b\u043a\u0430 \u0430 \u043d\u0435 \u041e\u0421, \u043a \u0442\u043e\u043c\u0443 \u0436\u0435 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0435\u0435 \u043c\u0435\u043d\u044c\u0448\u0435 \u0447\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u0443.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0415\u0441\u0442\u044c \u043b\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432\/\u043f\u043e\u0442\u043e\u043a\u043e\u0432\/\u043a\u043e\u0440\u0443\u0442\u0438\u043d?<\/h2>\n<p>\u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0432\u044b\u0448\u0435 \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u043b\u0438 \u0440\u0430\u0437\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447 \u0438 \u0443 \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u043e\u0433\u043e \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044f \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441: \u0421\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435? \u0427\u0435\u043c \u043c\u044b \u043f\u043b\u0430\u0442\u0438\u043c \u0437\u0430 \u0438\u0445 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435? \u041e\u0442\u0432\u0435\u0442 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0437\u0430\u0438\u0447\u0435\u043d: \u043c\u044b \u043f\u043b\u0430\u0442\u0438\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043d\u0430\u0448\u0435\u0439 \u043c\u0430\u0448\u0438\u043d\u044b. \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438 \u043f\u043e\u0442\u043e\u043a\u0430 \u041e\u0421 \u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u0443\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u043c\u044f\u0442\u0438, \u0430 \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0430\u043c\u044f\u0442\u044c \u043a\u043e\u043d\u0435\u0447\u043d\u0430 \u0442\u043e \u0431\u0435\u0437 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438\u043b\u0438 \u043f\u043e\u0442\u043e\u043a \u043c\u044b \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u0441\u0442\u043e\u043b\u043a\u043d\u0435\u043c\u0441\u044f \u0441 OOM (Out of memory error).<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0433\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u043e\u0448\u0438\u0431\u043e\u043a \u0432 production \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u0432 ThreadPool \u0438 ProcessPool.<\/p>\n<h2>\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d. \u041d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c \u0432 \u043a\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0442\u043e\u0442 \u0438\u043b\u0438 \u0438\u043d\u043e\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442. \u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u0434 \u0431\u044b\u043b\u0438 \u0432\u044b\u0431\u0440\u0430\u043d\u044b \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0441\u0438\u043d\u0442\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0430\u0449\u0438\u0435 production \u043a\u043e\u0434\u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u043c \u0435\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u043e \u0432 \u0440\u0430\u0431\u043e\u0442\u0435.<\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 <a href=\"https:\/\/github.com\/abstractart\/python-multiprocessing-example\" rel=\"noopener noreferrer nofollow\">GitHub<\/a>.<\/p>\n<p>\u0421\u0442\u0430\u0442\u044c\u0438 \u0434\u043b\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u043b\u0435\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/concurrent.futures.html\" rel=\"noopener noreferrer nofollow\">https:\/\/docs.python.org\/3\/library\/concurrent.futures.html<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/threading.html\" rel=\"noopener noreferrer nofollow\">https:\/\/docs.python.org\/3\/library\/threading.html<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.html\" rel=\"noopener noreferrer nofollow\">https:\/\/docs.python.org\/3\/library\/multiprocessing.html<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/superfastpython.com\/asyncio-vs-threading\/\" rel=\"noopener noreferrer nofollow\">https:\/\/superfastpython.com\/asyncio-vs-threading\/<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0447\u0442\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b\u0438 \u0434\u043e \u043a\u043e\u043d\u0446\u0430, \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u043b\u044e\u0431\u043e\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445!<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/773376\/\"> https:\/\/habr.com\/ru\/articles\/773376\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-359940","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/359940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=359940"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/359940\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=359940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=359940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=359940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}