{"id":331708,"date":"2022-04-08T21:00:12","date_gmt":"2022-04-08T21:00:12","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=331708"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=331708","title":{"rendered":"<span>Python: \u043f\u043e\u0442\u043e\u043a\u0438 \u043f\u043e-\u0434\u0440\u0443\u0433\u043e\u043c\u0443<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"float\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/14b\/103\/78f\/14b10378f79e481fd927fcc458eaa490.png\" width=\"450\" height=\"351\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/14b\/103\/78f\/14b10378f79e481fd927fcc458eaa490.png\"\/><figcaption><\/figcaption><\/figure>\n<h2>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0417\u043d\u0430\u0435\u0442\u0435, \u043f\u043e\u0447\u0435\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e? \u042f \u043f\u0438\u0441\u0430\u043b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443, \u0433\u0434\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u043f\u043e\u0442\u043e\u043a\u0438. \u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c\u0438 \u0432 <em>Python<\/em> \u0432\u0441\u0451 \u0431\u043e\u043b\u044c\u0448\u0435 \u0443\u0431\u0435\u0436\u0434\u0430\u0435\u0448\u044c \u0441\u0435\u0431\u044f, \u0447\u0442\u043e \u0442\u0443\u0442 \u0441 \u043d\u0438\u043c\u0438 \u0432\u0441\u0451 \u043f\u043b\u043e\u0445\u043e. \u041d\u0435\u0442, \u043d\u0435 \u0442\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u043f\u043b\u043e\u0445\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438. \u041f\u0440\u043e\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0445, \u043c\u044f\u0433\u043a\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e. \u042f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0443\u044e, \u043d\u043e \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443, \u0438 \u0437\u0434\u0435\u0441\u044c \u043f\u043e\u0434\u0435\u043b\u044e\u0441\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c.<\/p>\n<p>P.S.: \u0412 \u043a\u043e\u043d\u0446\u0435 \u043e\u0441\u0442\u0430\u0432\u043b\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 <em>GitHub<\/em><\/p>\n<h2>\u041f\u0435\u0440\u0432\u044b\u0435 \u0448\u0430\u0433\u0438<\/h2>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0441 \u0447\u0435\u0433\u043e \u044f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u0447\u0430\u0442\u044c &#8212; \u044d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0439. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0438\u0439 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u043d \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\"># thr.py from threading import Thread as thrd   __all__ = ['Thr']  # \u041a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u043b\u0438\u0431\u044b class Thr:   # \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440   def thread(fn):     def thr(*args, **kwargs):       thrd(target = fn, args = (*args,), kwargs={**kwargs,}).start()       pass     return thr   pass # \u043a\u043e\u043d\u0435\u0446 \u0444\u0430\u0439\u043b\u0430<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0438 \u0432\u0441\u0451. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u0438:<\/p>\n<pre><code class=\"python\">from thr import Thr   # \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f @Thr.thread def func1(a, b, c):   if a+b > c:     if a+c > b:       if b+c > a:         print(\"\u0442\u0440\u0435\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\")         pass       pass     pass   print(\"\u0442\u0440\u0435\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\")   pass # \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e\u043a\u0430 \u043d\u0435 \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e  for a, b, c in zip(range(1, 10), range(6, 15), range(11, 20)):   func1(a, b, c)   pass<\/code><\/pre>\n<p>\u0423\u0434\u043e\u0431\u043d\u0435\u0435 \u0447\u0435\u043c \u0431\u044b\u043b\u043e, \u0442\u0430\u043a \u0432\u0435\u0434\u044c? \u041d\u043e \u043c\u043d\u0435 \u0441\u0442\u0430\u043b\u043e \u043c\u0430\u043b\u043e.<\/p>\n<h2>\u0421\u0440\u0435\u0434\u044b \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/h2>\n<p>\u041c\u043d\u0435 \u0441\u0442\u0430\u043b\u043e \u043d\u0435 \u0443\u0434\u043e\u0431\u043d\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0442\u044c \u0432\u0435\u0440\u043d\u043e\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u043d\u0435 \u043d\u0430\u0434\u043e \u0447\u0442\u043e\u0431\u044b 2 \u0446\u0438\u043a\u043b\u0430 \u0440\u0435\u0448\u0430\u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0447\u0438\u0441\u0435\u043b \u0434\u043b\u044f <em>3n+1<\/em>. \u0422\u043e\u0433\u0434\u0430, \u043c\u043d\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043c\u0443\u0447\u0438\u0442\u044c <em>Python<\/em> \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438. \u0418 \u044f \u043d\u0430\u0448\u0435\u043b \u0432\u044b\u0445\u043e\u0434!<\/p>\n<p>P.S.: \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u044d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c <em>ThreadPoolExecutor<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u044f \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f. \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0442\u0430\u043a, \u0442\u043e, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u043c\u0443-\u0442\u043e \u043c\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0443\u0434\u043e\u0431\u043d\u0435\u0435.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u044e. \u0412\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0435 \u0441\u0440\u0435\u0434\u0443 \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432(\u043c\u043e\u0436\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e) \u0438 <strong>\u043d\u0435\u043c\u043d\u043e\u0433\u043e<\/strong> \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u0443\u0435\u0442\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0434 \u0441\u0435\u0431\u044f. \u043a\u043e\u0434 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:<\/p>\n<pre><code class=\"python\"># thr.py from curses.ascii import isalnum from threading import Thread as thrd from random import randrange from sys import exit as exitall   __all__ = ['Thr']  # f-str \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \"\\\" \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, # \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0432\u044b\u043a\u0440\u0443\u0447\u0438\u0432\u0430\u0442\u044c\u0441\u044f =) nl = \"\\n\" bs = \"\\b\" tb = \"\\t\" rt = \"\\r\"  # \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f def strcleanup(s: str = \"\"):     while s[0]  == ' ': s = s[1:]     while s[-1] == ' ': s = s[:-1]     if not isalnum(s[0]): s = '_' + s     s = s.replace(' ', '_')     for i in range(len(s)):         if not isalnum(s[i]):             s = s.replace(s[i], '_')             pass         pass     s += f\"{randrange(100, 999)}\"     return s  # \u041a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u043b\u0438\u0431\u044b class Thr:     # \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0441\u0440\u0435\u0434 \u043f\u043e\u0442\u043e\u043a\u043e\u0432     class Env(object):              # \u043f\u043e\u043b\u044f          # \u043f\u043e\u0442\u043e\u043a\u0438         thrs: list = None         # \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f         rets: dict = None         # \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0440\u0435\u0434\u044b         name: str  = None              # \u043c\u0435\u0442\u043e\u0434\u044b          # \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f         def __init__(self, name):             self.thrs = []             self.rets = {}             self.__name__ = self.name = name             # self.name \u043d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439.             # __name__ - \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f, \u0432\u0434\u0440\u0443\u0433 \u043f\u043e\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f.             pass          # \u0432 \u0441\u0442\u0440\u043e\u043a\u0443         __str__ = lambda self:\\         f\"\"\"ThreadSpace \"{self.name}\": {len(self.thrs)} threads\"\"\"                  # \u0442\u043e\u0436\u0435 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443, \u043d\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u0434\u043b\u044f \u0434\u0435\u0431\u0430\u0433\u0430, \u0447\u0435\u043c \u0434\u043b\u044f \u043f\u0435\u0447\u0430\u0442\u0438 \u044e\u0437\u0435\u0440\u0443         __repr__ = lambda self:\\         f\"\"\"ThreadSpace \"{self.name}\"     threads:        {(nl+\"       \").join(self.thrs)}     total: {len(self.thrs)} \"\"\"         def __add__(self, other):             self.thrs = {**self.thrs, **other.thrs}             pass          # \u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\/\u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0442\u043e\u043a\u043e\u0432.         def append(self, fn):             # \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0443\u0436\u0435\u043d docstring             ID = strcleanup(fn.__doc__.casefold())             self.thrs += [ID]             self.rets[ID] = None             #             class Thrd(object):                 ID = None                 space = None                 fn = None                 thr = None                 runned = None                 ret = None                 def __init__(slf, ID, self, fn):                     slf.ID = ID                     slf.space = self                     slf.fn = fn                     slf.thr = None                     slf.runned = False                     slf.ret = False                     pass                 def run(slf, *args):                     if slf.runned:                         print(f\"Exception: Thread \\\"{slf.ID[:-3]}\\\" of threadspace \\\"{slf.space.name}\\\" already started\")                         exitall(1)                         pass                     slf.thr = thrd(target = slf.fn, args = (slf, slf.space, slf.ID, *args,))                     slf.thr.start()                     slf.runned = True                     pass                 def join(slf):                     if not slf.runned:                         print(f\"Exception: Thread \\\"{slf.ID[:-3]}\\\" of threadspace \\\"{slf.space.name}\\\" not started yet\")                         exitall(1)                         pass                     slf.thr.join()                     slf.runned = False                     pass                 def get(slf):                     if not slf.ret:                         print(f\"Exception: Thread \\\"{slf.ID[:-3]}\\\" of threadspace \\\"{slf.space.name}\\\" didn`t return anything yet\")                         exitall(1)                         pass                     slf.runned = False                     return slf.space.rets[slf.ID]                 def getrun(slf, *args):                     slf.run(*args)                     slf.join()                     return slf.get()                 pass             return Thrd(ID, self, fn)         pass     #  \u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \"\u0433\u043e\u043b\u043e\u0433\u043e\" \u043f\u043e\u0442\u043e\u043a\u0430     def thread(fn):         def thr(*args, **kwargs):             thrd(target = fn, args = (*args,), kwargs={**kwargs,}).start()             pass         return thr     pass # \u043a\u043e\u043d\u0435\u0446 \u0444\u0430\u0439\u043b\u0430<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0432\u043e\u0434\u0430 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435:<\/p>\n<pre><code>Traceback (most recent call last):   File \"test.py\", line 37, in &lt;module>     loop.run()   File \"thr.py\", line 93, in run   raise Exception(...) Exception: Thread \"3n_1_mainloop\" of threadspace \"3n+1\" already started<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0431\u044b\u0441\u0442\u0440\u043e \u0432\u044b\u0440\u043e\u0441 \u043e\u0431\u044a\u0451\u043c \u043a\u043e\u0434\u0430 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u043c! \u0418\u0442\u0430\u043a, \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">from random import randint from thr import Thr   Space = Thr.Env(\"3n+1\")  @Space.append def hdl(t, spc, ID, num):     \"\"\"3n+1_handle\"\"\"     if num % 2 == 0:         # \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0442\u0430\u043a         t.ret = True         spc.rets[ID] = num\/2         return     # \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0442\u0430\u043a     t.ret = True     spc.rets[ID] = 3*num+1     return  @Space.append def loop(t, spc, ID, num):     \"\"\"3n+1_mainloop\"\"\"      steps = 0      while num not in (4, 2, 1):         num = hdl.getrun(num)         steps += 1         pass     # \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0442\u0430\u043a     t.ret = True     spc.rets[ID] = steps     return  print() print(Space) print() print(repr(Space))  ticks = 0  num = randint(5, 100)  loop.run(num)  while not loop.ret:     ticks += 1     pass  print(f\"loop reached 4 -> 2 -> 1 trap,\\n\"       f\"time has passed (loop ticks):\\n\"       f\"{ticks}, steps has passed: {loop.get()}, start number: {num}\")<\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434:<\/p>\n<pre><code>ThreadSpace \"3n+1\": 2 threads  ThreadSpace \"3n+1\"     threads:        3n_1_handle840        3n_1_mainloop515     total: 2  loop reached 4 -> 2 -> 1 trap, time has passed (loop ticks): 13606839, steps has passed: 33, start number: 78<\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u043d\u0430 \u044d\u0442\u043e\u043c \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0443 \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/p>\n<p><a href=\"https:\/\/github.com\/vanosoft\/ModernThreading\" rel=\"noopener noreferrer nofollow\">GitHub<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/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\/post\/659935\/\"> https:\/\/habr.com\/ru\/post\/659935\/<\/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_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"float\"><figcaption><\/figcaption><\/figure>\n<h2>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0417\u043d\u0430\u0435\u0442\u0435, \u043f\u043e\u0447\u0435\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e? \u042f \u043f\u0438\u0441\u0430\u043b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443, \u0433\u0434\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u043f\u043e\u0442\u043e\u043a\u0438. \u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c\u0438 \u0432 <em>Python<\/em> \u0432\u0441\u0451 \u0431\u043e\u043b\u044c\u0448\u0435 \u0443\u0431\u0435\u0436\u0434\u0430\u0435\u0448\u044c \u0441\u0435\u0431\u044f, \u0447\u0442\u043e \u0442\u0443\u0442 \u0441 \u043d\u0438\u043c\u0438 \u0432\u0441\u0451 \u043f\u043b\u043e\u0445\u043e. \u041d\u0435\u0442, \u043d\u0435 \u0442\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u043f\u043b\u043e\u0445\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438. \u041f\u0440\u043e\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0445, \u043c\u044f\u0433\u043a\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e. \u042f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0443\u044e, \u043d\u043e \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443, \u0438 \u0437\u0434\u0435\u0441\u044c \u043f\u043e\u0434\u0435\u043b\u044e\u0441\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c.<\/p>\n<p>P.S.: \u0412 \u043a\u043e\u043d\u0446\u0435 \u043e\u0441\u0442\u0430\u0432\u043b\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 <em>GitHub<\/em><\/p>\n<h2>\u041f\u0435\u0440\u0432\u044b\u0435 \u0448\u0430\u0433\u0438<\/h2>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0441 \u0447\u0435\u0433\u043e \u044f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u0447\u0430\u0442\u044c &#8212; \u044d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0439. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0438\u0439 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u043d \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\"># thr.py from threading import Thread as thrd   __all__ = ['Thr']  # \u041a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u043b\u0438\u0431\u044b class Thr:   # \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440   def thread(fn):     def thr(*args, **kwargs):       thrd(target = fn, args = (*args,), kwargs={**kwargs,}).start()       pass     return thr   pass # \u043a\u043e\u043d\u0435\u0446 \u0444\u0430\u0439\u043b\u0430<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0438 \u0432\u0441\u0451. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u0438:<\/p>\n<pre><code class=\"python\">from thr import Thr   # \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f @Thr.thread def func1(a, b, c):   if a+b > c:     if a+c > b:       if b+c > a:         print(\"\u0442\u0440\u0435\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\")         pass       pass     pass   print(\"\u0442\u0440\u0435\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\")   pass # \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e\u043a\u0430 \u043d\u0435 \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e  for a, b, c in zip(range(1, 10), range(6, 15), range(11, 20)):   func1(a, b, c)   pass<\/code><\/pre>\n<p>\u0423\u0434\u043e\u0431\u043d\u0435\u0435 \u0447\u0435\u043c \u0431\u044b\u043b\u043e, \u0442\u0430\u043a \u0432\u0435\u0434\u044c? \u041d\u043e \u043c\u043d\u0435 \u0441\u0442\u0430\u043b\u043e \u043c\u0430\u043b\u043e.<\/p>\n<h2>\u0421\u0440\u0435\u0434\u044b \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/h2>\n<p>\u041c\u043d\u0435 \u0441\u0442\u0430\u043b\u043e \u043d\u0435 \u0443\u0434\u043e\u0431\u043d\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0442\u044c \u0432\u0435\u0440\u043d\u043e\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u043d\u0435 \u043d\u0430\u0434\u043e \u0447\u0442\u043e\u0431\u044b 2 \u0446\u0438\u043a\u043b\u0430 \u0440\u0435\u0448\u0430\u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0447\u0438\u0441\u0435\u043b \u0434\u043b\u044f <em>3n+1<\/em>. \u0422\u043e\u0433\u0434\u0430, \u043c\u043d\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043c\u0443\u0447\u0438\u0442\u044c <em>Python<\/em> \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438. \u0418 \u044f \u043d\u0430\u0448\u0435\u043b \u0432\u044b\u0445\u043e\u0434!<\/p>\n<p>P.S.: \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u044d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c <em>ThreadPoolExecutor<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u044f \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f. \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0442\u0430\u043a, \u0442\u043e, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u043c\u0443-\u0442\u043e \u043c\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0443\u0434\u043e\u0431\u043d\u0435\u0435.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u044e. \u0412\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0435 \u0441\u0440\u0435\u0434\u0443 \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432(\u043c\u043e\u0436\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e) \u0438 <strong>\u043d\u0435\u043c\u043d\u043e\u0433\u043e<\/strong> \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u0443\u0435\u0442\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0434 \u0441\u0435\u0431\u044f. \u043a\u043e\u0434 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:<\/p>\n<pre><code class=\"python\"># thr.py from curses.ascii import isalnum from threading import Thread as thrd from random import randrange from sys import exit as exitall   __all__ = ['Thr']  # f-str \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \"\\\" \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, # \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0432\u044b\u043a\u0440\u0443\u0447\u0438\u0432\u0430\u0442\u044c\u0441\u044f =) nl = \"\\n\" bs = \"\\b\" tb = \"\\t\" rt = \"\\r\"  # \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f def strcleanup(s: str = \"\"):     while s[0]  == ' ': s = s[1:]     while s[-1] == ' ': s = s[:-1]     if not isalnum(s[0]): s = '_' + s     s = s.replace(' ', '_')     for i in range(len(s)):         if not isalnum(s[i]):             s = s.replace(s[i], '_')             pass         pass     s += f\"{randrange(100, 999)}\"     return s  # \u041a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u043b\u0438\u0431\u044b class Thr:     # \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0441\u0440\u0435\u0434 \u043f\u043e\u0442\u043e\u043a\u043e\u0432     class Env(object):              # \u043f\u043e\u043b\u044f          # \u043f\u043e\u0442\u043e\u043a\u0438         thrs: list = None         # \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f         rets: dict = None         # \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0440\u0435\u0434\u044b         name: str  = None              # \u043c\u0435\u0442\u043e\u0434\u044b          # \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f         def __init__(self, name):             self.thrs = []             self.rets = {}             self.__name__ = self.name = name             # self.name \u043d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439.             # __name__ - \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f, \u0432\u0434\u0440\u0443\u0433 \u043f\u043e\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f.             pass          # \u0432 \u0441\u0442\u0440\u043e\u043a\u0443         __str__ = lambda self:\\         f\"\"\"ThreadSpace \"{self.name}\": {len(self.thrs)} threads\"\"\"                  # \u0442\u043e\u0436\u0435 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443, \u043d\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u0434\u043b\u044f \u0434\u0435\u0431\u0430\u0433\u0430, \u0447\u0435\u043c \u0434\u043b\u044f \u043f\u0435\u0447\u0430\u0442\u0438 \u044e\u0437\u0435\u0440\u0443         __repr__ = lambda self:\\         f\"\"\"ThreadSpace \"{self.name}\"     threads:        {(nl+\"       \").join(self.thrs)}     total: {len(self.thrs)} \"\"\"         def __add__(self, other):             self.thrs = {**self.thrs, **other.thrs}             pass          # \u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\/\u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0442\u043e\u043a\u043e\u0432.         def append(self, fn):             # \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0443\u0436\u0435\u043d docstring             ID = strcleanup(fn.__doc__.casefold())             self.thrs += [ID]             self.rets[ID] = None             #             class Thrd(object):                 ID = None                 space = None                 fn = None                 thr = None                 runned = None                 ret = None                 def __init__(slf, ID, self, fn):                     slf.ID = ID                     slf.space = self                     slf.fn = fn                     slf.thr = None                     slf.runned = False                     slf.ret = False                     pass                 def run(slf, *args):                     if slf.runned:                         print(f\"Exception: Thread \\\"{slf.ID[:-3]}\\\" of threadspace \\\"{slf.space.name}\\\" already started\")                         exitall(1)                         pass                     slf.thr = thrd(target = slf.fn, args = (slf, slf.space, slf.ID, *args,))                     slf.thr.start()                     slf.runned = True                     pass                 def join(slf):                     if not slf.runned:                         print(f\"Exception: Thread \\\"{slf.ID[:-3]}\\\" of threadspace \\\"{slf.space.name}\\\" not started yet\")                         exitall(1)                         pass                     slf.thr.join()                     slf.runned = False                     pass                 def get(slf):                     if not slf.ret:                         print(f\"Exception: Thread \\\"{slf.ID[:-3]}\\\" of threadspace \\\"{slf.space.name}\\\" didn`t return anything yet\")                         exitall(1)                         pass                     slf.runned = False                     return slf.space.rets[slf.ID]                 def getrun(slf, *args):                     slf.run(*args)                     slf.join()                     return slf.get()                 pass             return Thrd(ID, self, fn)         pass     #  \u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \"\u0433\u043e\u043b\u043e\u0433\u043e\" \u043f\u043e\u0442\u043e\u043a\u0430     def thread(fn):         def thr(*args, **kwargs):             thrd(target = fn, args = (*args,), kwargs={**kwargs,}).start()             pass         return thr     pass # \u043a\u043e\u043d\u0435\u0446 \u0444\u0430\u0439\u043b\u0430<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0432\u043e\u0434\u0430 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435:<\/p>\n<pre><code>Traceback (most recent call last):   File \"test.py\", line 37, in &lt;module>     loop.run()   File \"thr.py\", line 93, in run   raise Exception(...) Exception: Thread \"3n_1_mainloop\" of threadspace \"3n+1\" already started<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0431\u044b\u0441\u0442\u0440\u043e \u0432\u044b\u0440\u043e\u0441 \u043e\u0431\u044a\u0451\u043c \u043a\u043e\u0434\u0430 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u043c! \u0418\u0442\u0430\u043a, \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">from random import randint from thr import Thr   Space = Thr.Env(\"3n+1\")  @Space.append def hdl(t, spc, ID, num):     \"\"\"3n+1_handle\"\"\"     if num % 2 == 0:         # \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0442\u0430\u043a         t.ret = True         spc.rets[ID] = num\/2         return     # \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0442\u0430\u043a     t.ret = True     spc.rets[ID] = 3*num+1     return  @Space.append def loop(t, spc, ID, num):     \"\"\"3n+1_mainloop\"\"\"      steps = 0      while num not in (4, 2, 1):         num = hdl.getrun(num)         steps += 1         pass     # \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0442\u0430\u043a     t.ret = True     spc.rets[ID] = steps     return  print() print(Space) print() print(repr(Space))  ticks = 0  num = randint(5, 100)  loop.run(num)  while not loop.ret:     ticks += 1     pass  print(f\"loop reached 4 -> 2 -> 1 trap,\\n\"       f\"time has passed (loop ticks):\\n\"       f\"{ticks}, steps has passed: {loop.get()}, start number: {num}\")<\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434:<\/p>\n<pre><code>ThreadSpace \"3n+1\": 2 threads  ThreadSpace \"3n+1\"     threads:        3n_1_handle840        3n_1_mainloop515     total: 2  loop reached 4 -> 2 -> 1 trap, time has passed (loop ticks): 13606839, steps has passed: 33, start number: 78<\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u043d\u0430 \u044d\u0442\u043e\u043c \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0443 \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/p>\n<p><a href=\"https:\/\/github.com\/vanosoft\/ModernThreading\" rel=\"noopener noreferrer nofollow\">GitHub<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/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\/post\/659935\/\"> https:\/\/habr.com\/ru\/post\/659935\/<\/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-331708","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/331708","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=331708"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/331708\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=331708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=331708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=331708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}