{"id":356257,"date":"2024-05-20T23:25:47","date_gmt":"2024-05-20T23:25:47","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=356257"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-20T23:25:47","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=356257","title":{"rendered":"<span>\u041c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0432 Python: \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0435 \u0438 \u043d\u0435\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0435<\/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>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043a\u0430\u043a \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0432 Python, \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043f\u0440\u043e \u043f\u043e\u0442\u043e\u043a\u0438, \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u043e \u0442\u043e\u043c \u0437\u0430\u0447\u0435\u043c \u043e\u043d\u0438 \u043d\u0443\u0436\u043d\u044b.<\/p>\n<p>\u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u044f \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043b \u0447\u0442\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u0430\u044f \u0437\u0430\u043c\u0435\u0442\u043a\u0430, \u043d\u043e \u043f\u043e\u043a\u0430 \u0433\u043e\u0442\u043e\u0432\u0438\u043b \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043b \u043a\u043e\u0434 \u043d\u0430\u0448\u0435\u043b \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0439 \u0441 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u043c\u0438 CPython, \u0442\u0430\u043a \u0447\u0442\u043e \u043d\u0435 \u0441\u043f\u0435\u0448\u0438\u0442\u0435 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0432\u043a\u043b\u0430\u0434\u043a\u0443, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0443\u0432\u0435\u0440\u0435\u043d\u044b \u0447\u0442\u043e \u0437\u043d\u0430\u0435\u0442\u0435 \u043e \u043f\u043e\u0442\u043e\u043a\u0430\u0445 \u0432 Python \u0432\u0441\u0451 \ud83d\ude42<\/p>\n<h3>\u041a\u043e\u0434<\/h3>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0447\u0442\u043e \u043d\u0430\u043c \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043d\u0443\u0436\u0435\u043d \u0441\u0447\u0435\u0442\u0447\u0438\u043a. \u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b, \u043d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e:<\/p>\n<pre><code class=\"python\">class Counter:   def __init__(self):     self.val = 0      def change(self):     self.val += 1<\/code><\/pre>\n<p>\u0418\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043c\u044b \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u043c \u0438\u0437 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 X \u0440\u0430\u0437<\/p>\n<pre><code class=\"python\">def work(counter, operationsCount):   for _ in range(operationsCount):       counter.change()  def run_threads(counter, threadsCount, operationsPerThreadCount):   threads = []      for _ in range(threadsCount):     t = threading.Thread(target=work, args=(counter, operationsPerThreadCount))     t.start()     threads.append(t)      for t in threads:     t.join() <\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u201cmain\u201d \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">if __name__ == \"__main__\":     threadsCount = 10   operationsPerThreadCount = 1000000    expectedCounterValue = threadsCount * operationsPerThreadCount   counters = [Counter()]      for counter in counters:     run_threads(counter, threadsCount, operationsPerThreadCount)     print(f\"{counter.class.name}: expected val: {expectedCounterValue}, actual val: {counter.val}\") <\/code><\/pre>\n<p><strong>\u0412\u043e\u043f\u0440\u043e\u0441<\/strong>: \u043a\u0430\u043a\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0432\u044b\u0432\u0435\u0434\u0435\u0442 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430?<\/p>\n<details class=\"spoiler\">\n<summary>\u041e\u0442\u0432\u0435\u0442 <\/summary>\n<div class=\"spoiler__content\">\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0432\u0435\u0440\u0441\u0438\u0438 Python \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u044b\u043b \u0437\u0430\u043f\u0443\u0449\u0435\u043d \u0441\u043a\u0440\u0438\u043f\u0442.<\/p>\n<\/div>\n<\/details>\n<p>\u041a\u043e\u0433\u0434\u0430 \u044f \u0432 \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b \u044d\u0442\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u044f \u0431\u044b\u043b \u043e\u0448\u0430\u0440\u0430\u0448\u0435\u043d \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c\u0438, \u044f \u0431\u044b\u043b \u0443\u0432\u0435\u0440\u0435\u043d \u043d\u0430 100% \u0447\u0442\u043e \u0443\u0432\u0438\u0436\u0443 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043d\u0430 Python 3.11.5:<\/p>\n<pre><code class=\"python\">Counter: expected val: 10000000, actual val: 10000000<\/code><\/pre>\n<p>CPython \u043d\u0435\u0432\u0435\u0434\u043e\u043c\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u0441\u043c\u043e\u0433 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0441\u0442\u044c \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0439 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 increment.<\/p>\n<p>\u041a\u0430\u043a \u043e\u043d \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043b?  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f.<\/p>\n<h3>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 Python<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u043f\u043e\u0433\u0440\u0443\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u0434\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0438 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0430 \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 \u044f\u0437\u044b\u043a\u0430. \u0412 \u044d\u0442\u043e\u043c \u043c\u043d\u0435 \u0437\u0434\u043e\u0440\u043e\u0432\u043e \u043f\u043e\u043c\u043e\u0433\u043b\u0430 \u0443\u0442\u0438\u043b\u0438\u0442\u0430 <a href=\"https:\/\/github.com\/pyenv\/pyenv\" rel=\"noopener noreferrer nofollow\">pyenv<\/a><\/p>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 Python<\/p>\n<pre><code class=\"bash\">#!\/bin\/bash versions=(3.7 3.8 3.9 3.10 3.11) for version in ${versions[*]} do   pyenv shell $version   python3 --version   python3 main.py   echo '\\n' done <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<pre><code class=\"python\">Python 3.7.17 Counter: expected val: 10000000, actual val: 4198551  Python 3.8.18 Counter: expected val: 10000000, actual val: 4999351  Python 3.9.18 Counter: expected val: 10000000, actual val: 3551269  Python 3.10.13 Counter: expected val: 10000000, actual val: 10000000  Python 3.11.5 Counter: expected val: 10000000, actual val: 10000000<\/code><\/pre>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u0432 \u043e\u0434\u043d\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 Python \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c \u0430 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043d\u0435\u0442? \u0412\u0441\u0435\u043c\u0443 \u0432\u0438\u043d\u043e\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u043e\u043d\u043a\u0438.<\/p>\n<h3>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u043e\u043d\u043a\u0438 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 increment<\/h3>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u0441 \u043d\u0430\u0448\u0438\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u043c \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0433\u043e\u043d\u043a\u0438? \u0412\u0441\u0451 \u0434\u0435\u043b\u043e \u0432 \u0442\u043e\u043c \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f increment \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0448\u0430\u0433\u043e\u0432:<\/p>\n<ul>\n<li>\n<p>\u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (currVal = self.val)<\/p>\n<\/li>\n<li>\n<p>\u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c (newVal =currVal  + 1)<\/p>\n<\/li>\n<li>\n<p>\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (self.val = newVal)<\/p>\n<\/li>\n<\/ul>\n<p>\u0418 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u0448\u0430\u0433\u0430 1 \u0438\u043b\u0438 \u0448\u0430\u0433\u0430 2 , \u0447\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u0442\u043e\u043a \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u0448\u0430\u0433\u0430 3 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0432 \u0441\u0432\u043e\u0435\u043c \u0440\u0430\u0441\u043f\u043e\u0440\u044f\u0436\u0435\u043d\u0438\u0438 \u043d\u0435\u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/834\/c58\/794\/834c58794a82dd49999950b9299ed4c0.png\" alt=\"\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u043e\u043d\u043a\u0438 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 2\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\" title=\"\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u043e\u043d\u043a\u0438 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 2\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\" width=\"1148\" height=\"638\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/834\/c58\/794\/834c58794a82dd49999950b9299ed4c0.png\"\/><\/p>\n<div><figcaption><code>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u043e\u043d\u043a\u0438 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 2\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/code><\/figcaption><\/div>\n<\/figure>\n<h3>\u041f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0439 \u0438\u0442\u043e\u0433<\/h3>\n<p>\u041c\u043e\u0436\u043d\u043e \u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0432\u043e\u0434 \u0447\u0442\u043e \u0432 Python 3.10 \u0438\u0437\u0431\u0430\u0432\u0438\u043b\u0438\u0441\u044c \u043e\u0442 race condition \u0438 \u043d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u044b \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438? \u041a\u0430\u043a \u0431\u044b \u043d\u0435 \u0442\u0430\u043a \ud83d\ude42<\/p>\n<p>\u041f\u0440\u043e\u0432\u0435\u0434\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0440\u0430\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u044f \u043d\u0430\u0448\u0435\u043b \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 <a href=\"https:\/\/github.com\/python\/cpython\/commit\/4958f5d69dd2bf86866c43491caf72f774ddec97\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u043c\u043c\u0438\u0442<\/a> \u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 <a href=\"https:\/\/twitter.com\/Yhg1s\/status\/1460935209059328000\" rel=\"noopener noreferrer nofollow\">\u0442\u0432\u0438\u0442\u0442\u0435\u0440\u0435<\/a> \u043e\u0442 Python Core Developer.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/165\/87d\/025\/16587d025bc8f9fd22e446cd80032d1b.PNG\" alt=\"tweet.PNG\" title=\" \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 \u0442\u0432\u0438\u0442\u0442\u0435\u0440\u0435 \u043e\u0442 Python Core Developer.\" width=\"738\" height=\"784\"\/><\/p>\n<div><figcaption> \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 <a href=\"https:\/\/twitter.com\/Yhg1s\/status\/1460935209059328000\" rel=\"noopener noreferrer nofollow\">\u0442\u0432\u0438\u0442\u0442\u0435\u0440\u0435<\/a> \u043e\u0442 Python Core Developer.<\/figcaption><\/div>\n<\/figure>\n<h3>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b<\/h3>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e  \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430, \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0449\u0443\u044e\u0441\u044f \u043e\u0442 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u043e\u0439:<\/p>\n<pre><code class=\"python\">class CounterWithConversion:   def __init__(self):     self.val = 0      def change(self):     self.val += int(1) # \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 - \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0438\u043f\u0430 <\/code><\/pre>\n<p>\u0418 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0442\u0435\u0441\u0442\u044b:<\/p>\n<pre><code class=\"python\">Python 3.7.17 CounterWithConversion: expected val: 10000000, actual val: 1960102  Python 3.8.18 CounterWithConversion: expected val: 10000000, actual val: 2860607  Python 3.9.18 CounterWithConversion: expected val: 10000000, actual val: 2558964  Python 3.10.13 CounterWithConversion: expected val: 10000000, actual val: 3387681  Python 3.11.5 CounterWithConversion: expected val: 10000000, actual val: 2310891<\/code><\/pre>\n<p>\u0412\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u043b\u043e\u043c\u0430\u0435\u0442 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u0436\u0435 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 Python.<\/p>\n<h3>\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u0430<\/h3>\n<p>\u041c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b\u0438 \u0440\u0430\u0437\u043d\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u0440\u0430\u0437\u043d\u044b\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 Python \u0438 \u0432\u0435\u0437\u0434\u0435 \u0431\u044b\u043b\u0438 \u0441\u0432\u043e\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u0442\u044c \u0442\u043e\u0447\u043d\u043e \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u043c\u0438 \u0432 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0435 \u0442\u043e \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044e, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u0433\u043e\u043d\u043a\u0438 \u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0435:<\/p>\n<pre><code class=\"python\">class ThreadSafeCounter:   def __init__(self):     self.val = 0     self.lock = threading.Lock()    def change(self):     with self.lock:       self.val += 1<\/code><\/pre>\n<p><strong>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b<\/strong><\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0431\u0435\u0437 \u0441\u044e\u0440\u043f\u0440\u0438\u0437\u043e\u0432 \ud83d\ude42<\/p>\n<pre><code class=\"python\">Python 3.7.17 ThreadSafeCounter: expected val: 1000000, actual val: 1000000  Python 3.8.18 ThreadSafeCounter: expected val: 1000000, actual val: 1000000  Python 3.9.18 ThreadSafeCounter: expected val: 1000000, actual val: 1000000  Python 3.10.13 ThreadSafeCounter: expected val: 1000000, actual val: 1000000  Python 3.11.5 ThreadSafeCounter: expected val: 1000000, actual val: 1000000<\/code><\/pre>\n<h3>\u0418\u0442\u043e\u0433\u0438<\/h3>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u043b\u0441\u044f \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043f\u043e\u0442\u043e\u043a\u0438, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u043e\u043d\u043a\u0438 \u0438 \u043a\u0430\u043a \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0435\u0433\u043e \u0438\u0437\u0431\u0435\u0433\u0430\u0442\u044c \u0430 \u0442\u0430\u043a\u0436\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u043b \u043f\u0440\u043e \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u0443\u044e \u0431\u0430\u0433-\u0444\u0438\u0447\u0443 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u043e \u044f \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b \u0432\u0435\u0441\u044c \u043a\u043e\u0434 \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0430 <a href=\"https:\/\/github.com\/abstractart\/python-race-condition-example\" rel=\"noopener noreferrer nofollow\">GitHub<\/a>.<\/p>\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, \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u0447\u0442\u043e \u0432\u0430\u043c \u0431\u044b\u043b\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e!<\/p>\n<p>\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/docs.python.org\/3\/faq\/library.html#what-kinds-of-global-value-mutation-are-thread-safe\" rel=\"noopener noreferrer nofollow\">What kinds of global value mutation are thread-safe?<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/superfastpython.com\/context-switch-interval-in-python\/\" rel=\"noopener noreferrer nofollow\">Context Switch Interval In Python<\/a><\/p>\n<\/li>\n<\/ul>\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\/764420\/\"> https:\/\/habr.com\/ru\/articles\/764420\/<\/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>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043a\u0430\u043a \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0432 Python, \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043f\u0440\u043e \u043f\u043e\u0442\u043e\u043a\u0438, \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u043e \u0442\u043e\u043c \u0437\u0430\u0447\u0435\u043c \u043e\u043d\u0438 \u043d\u0443\u0436\u043d\u044b.<\/p>\n<p>\u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u044f \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043b \u0447\u0442\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u0430\u044f \u0437\u0430\u043c\u0435\u0442\u043a\u0430, \u043d\u043e \u043f\u043e\u043a\u0430 \u0433\u043e\u0442\u043e\u0432\u0438\u043b \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043b \u043a\u043e\u0434 \u043d\u0430\u0448\u0435\u043b \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0439 \u0441 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u043c\u0438 CPython, \u0442\u0430\u043a \u0447\u0442\u043e \u043d\u0435 \u0441\u043f\u0435\u0448\u0438\u0442\u0435 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0432\u043a\u043b\u0430\u0434\u043a\u0443, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0443\u0432\u0435\u0440\u0435\u043d\u044b \u0447\u0442\u043e \u0437\u043d\u0430\u0435\u0442\u0435 \u043e \u043f\u043e\u0442\u043e\u043a\u0430\u0445 \u0432 Python \u0432\u0441\u0451 \ud83d\ude42<\/p>\n<h3>\u041a\u043e\u0434<\/h3>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0447\u0442\u043e \u043d\u0430\u043c \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043d\u0443\u0436\u0435\u043d \u0441\u0447\u0435\u0442\u0447\u0438\u043a. \u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b, \u043d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e:<\/p>\n<pre><code class=\"python\">class Counter:   def __init__(self):     self.val = 0      def change(self):     self.val += 1<\/code><\/pre>\n<p>\u0418\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043c\u044b \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u043c \u0438\u0437 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 X \u0440\u0430\u0437<\/p>\n<pre><code class=\"python\">def work(counter, operationsCount):   for _ in range(operationsCount):       counter.change()  def run_threads(counter, threadsCount, operationsPerThreadCount):   threads = []      for _ in range(threadsCount):     t = threading.Thread(target=work, args=(counter, operationsPerThreadCount))     t.start()     threads.append(t)      for t in threads:     t.join() <\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u201cmain\u201d \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">if __name__ == \"__main__\":     threadsCount = 10   operationsPerThreadCount = 1000000    expectedCounterValue = threadsCount * operationsPerThreadCount   counters = [Counter()]      for counter in counters:     run_threads(counter, threadsCount, operationsPerThreadCount)     print(f\"{counter.class.name}: expected val: {expectedCounterValue}, actual val: {counter.val}\") <\/code><\/pre>\n<p><strong>\u0412\u043e\u043f\u0440\u043e\u0441<\/strong>: \u043a\u0430\u043a\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0432\u044b\u0432\u0435\u0434\u0435\u0442 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430?<\/p>\n<details class=\"spoiler\">\n<summary>\u041e\u0442\u0432\u0435\u0442 <\/summary>\n<div class=\"spoiler__content\">\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0432\u0435\u0440\u0441\u0438\u0438 Python \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u044b\u043b \u0437\u0430\u043f\u0443\u0449\u0435\u043d \u0441\u043a\u0440\u0438\u043f\u0442.<\/p>\n<\/div>\n<\/details>\n<p>\u041a\u043e\u0433\u0434\u0430 \u044f \u0432 \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b \u044d\u0442\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u044f \u0431\u044b\u043b \u043e\u0448\u0430\u0440\u0430\u0448\u0435\u043d \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c\u0438, \u044f \u0431\u044b\u043b \u0443\u0432\u0435\u0440\u0435\u043d \u043d\u0430 100% \u0447\u0442\u043e \u0443\u0432\u0438\u0436\u0443 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043d\u0430 Python 3.11.5:<\/p>\n<pre><code class=\"python\">Counter: expected val: 10000000, actual val: 10000000<\/code><\/pre>\n<p>CPython \u043d\u0435\u0432\u0435\u0434\u043e\u043c\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u0441\u043c\u043e\u0433 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0441\u0442\u044c \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0439 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 increment.<\/p>\n<p>\u041a\u0430\u043a \u043e\u043d \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043b?  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f.<\/p>\n<h3>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 Python<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u043f\u043e\u0433\u0440\u0443\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u0434\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0438 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0430 \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 \u044f\u0437\u044b\u043a\u0430. \u0412 \u044d\u0442\u043e\u043c \u043c\u043d\u0435 \u0437\u0434\u043e\u0440\u043e\u0432\u043e \u043f\u043e\u043c\u043e\u0433\u043b\u0430 \u0443\u0442\u0438\u043b\u0438\u0442\u0430 <a href=\"https:\/\/github.com\/pyenv\/pyenv\" rel=\"noopener noreferrer nofollow\">pyenv<\/a><\/p>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 Python<\/p>\n<pre><code class=\"bash\">#!\/bin\/bash versions=(3.7 3.8 3.9 3.10 3.11) for version in ${versions[*]} do   pyenv shell $version   python3 --version   python3 main.py   echo '\\n' done <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<pre><code class=\"python\">Python 3.7.17 Counter: expected val: 10000000, actual val: 4198551  Python 3.8.18 Counter: expected val: 10000000, actual val: 4999351  Python 3.9.18 Counter: expected val: 10000000, actual val: 3551269  Python 3.10.13 Counter: expected val: 10000000, actual val: 10000000  Python 3.11.5 Counter: expected val: 10000000, actual val: 10000000<\/code><\/pre>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u0432 \u043e\u0434\u043d\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 Python \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c \u0430 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043d\u0435\u0442? \u0412\u0441\u0435\u043c\u0443 \u0432\u0438\u043d\u043e\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u043e\u043d\u043a\u0438.<\/p>\n<h3>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u043e\u043d\u043a\u0438 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 increment<\/h3>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u0441 \u043d\u0430\u0448\u0438\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u043c \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0433\u043e\u043d\u043a\u0438? \u0412\u0441\u0451 \u0434\u0435\u043b\u043e \u0432 \u0442\u043e\u043c \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f increment \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0448\u0430\u0433\u043e\u0432:<\/p>\n<ul>\n<li>\n<p>\u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (currVal = self.val)<\/p>\n<\/li>\n<li>\n<p>\u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c (newVal =currVal  + 1)<\/p>\n<\/li>\n<li>\n<p>\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (self.val = newVal)<\/p>\n<\/li>\n<\/ul>\n<p>\u0418 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u0448\u0430\u0433\u0430 1 \u0438\u043b\u0438 \u0448\u0430\u0433\u0430 2 , \u0447\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u0442\u043e\u043a \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u0448\u0430\u0433\u0430 3 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0432 \u0441\u0432\u043e\u0435\u043c \u0440\u0430\u0441\u043f\u043e\u0440\u044f\u0436\u0435\u043d\u0438\u0438 \u043d\u0435\u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<figure class=\"full-width\">\n<div><figcaption><code>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u043e\u043d\u043a\u0438 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 2\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/code><\/figcaption><\/div>\n<\/figure>\n<h3>\u041f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0439 \u0438\u0442\u043e\u0433<\/h3>\n<p>\u041c\u043e\u0436\u043d\u043e \u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0432\u043e\u0434 \u0447\u0442\u043e \u0432 Python 3.10 \u0438\u0437\u0431\u0430\u0432\u0438\u043b\u0438\u0441\u044c \u043e\u0442 race condition \u0438 \u043d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u044b \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438? \u041a\u0430\u043a \u0431\u044b \u043d\u0435 \u0442\u0430\u043a \ud83d\ude42<\/p>\n<p>\u041f\u0440\u043e\u0432\u0435\u0434\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0440\u0430\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u044f \u043d\u0430\u0448\u0435\u043b \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 <a href=\"https:\/\/github.com\/python\/cpython\/commit\/4958f5d69dd2bf86866c43491caf72f774ddec97\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u043c\u043c\u0438\u0442<\/a> \u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 <a href=\"https:\/\/twitter.com\/Yhg1s\/status\/1460935209059328000\" rel=\"noopener noreferrer nofollow\">\u0442\u0432\u0438\u0442\u0442\u0435\u0440\u0435<\/a> \u043e\u0442 Python Core Developer.<\/p>\n<figure class=\"full-width\">\n<div><figcaption> \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 <a href=\"https:\/\/twitter.com\/Yhg1s\/status\/1460935209059328000\" rel=\"noopener noreferrer nofollow\">\u0442\u0432\u0438\u0442\u0442\u0435\u0440\u0435<\/a> \u043e\u0442 Python Core Developer.<\/figcaption><\/div>\n<\/figure>\n<h3>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b<\/h3>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e  \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430, \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0449\u0443\u044e\u0441\u044f \u043e\u0442 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u043e\u0439:<\/p>\n<pre><code class=\"python\">class CounterWithConversion:   def __init__(self):     self.val = 0      def change(self):     self.val += int(1) # \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 - \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0438\u043f\u0430 <\/code><\/pre>\n<p>\u0418 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0442\u0435\u0441\u0442\u044b:<\/p>\n<pre><code class=\"python\">Python 3.7.17 CounterWithConversion: expected val: 10000000, actual val: 1960102  Python 3.8.18 CounterWithConversion: expected val: 10000000, actual val: 2860607  Python 3.9.18 CounterWithConversion: expected val: 10000000, actual val: 2558964  Python 3.10.13 CounterWithConversion: expected val: 10000000, actual val: 3387681  Python 3.11.5 CounterWithConversion: expected val: 10000000, actual val: 2310891<\/code><\/pre>\n<p>\u0412\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u043b\u043e\u043c\u0430\u0435\u0442 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u0436\u0435 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 Python.<\/p>\n<h3>\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u0430<\/h3>\n<p>\u041c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b\u0438 \u0440\u0430\u0437\u043d\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u0440\u0430\u0437\u043d\u044b\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 Python \u0438 \u0432\u0435\u0437\u0434\u0435 \u0431\u044b\u043b\u0438 \u0441\u0432\u043e\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u0442\u044c \u0442\u043e\u0447\u043d\u043e \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u043c\u0438 \u0432 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0435 \u0442\u043e \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044e, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u0433\u043e\u043d\u043a\u0438 \u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0435:<\/p>\n<pre><code class=\"python\">class ThreadSafeCounter:   def __init__(self):     self.val = 0     self.lock = threading.Lock()    def change(self):     with self.lock:       self.val += 1<\/code><\/pre>\n<p><strong>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b<\/strong><\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0431\u0435\u0437 \u0441\u044e\u0440\u043f\u0440\u0438\u0437\u043e\u0432 \ud83d\ude42<\/p>\n<pre><code class=\"python\">Python 3.7.17 ThreadSafeCounter: expected val: 1000000, actual val: 1000000  Python 3.8.18 ThreadSafeCounter: expected val: 1000000, actual val: 1000000  Python 3.9.18 ThreadSafeCounter: expected val: 1000000, actual val: 1000000  Python 3.10.13 ThreadSafeCounter: expected val: 1000000, actual val: 1000000  Python 3.11.5 ThreadSafeCounter: expected val: 1000000, actual val: 1000000<\/code><\/pre>\n<h3>\u0418\u0442\u043e\u0433\u0438<\/h3>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u043b\u0441\u044f \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043f\u043e\u0442\u043e\u043a\u0438, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u043e\u043d\u043a\u0438 \u0438 \u043a\u0430\u043a \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0435\u0433\u043e \u0438\u0437\u0431\u0435\u0433\u0430\u0442\u044c \u0430 \u0442\u0430\u043a\u0436\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u043b \u043f\u0440\u043e \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u0443\u044e \u0431\u0430\u0433-\u0444\u0438\u0447\u0443 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u043e \u044f \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b \u0432\u0435\u0441\u044c \u043a\u043e\u0434 \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0430 <a href=\"https:\/\/github.com\/abstractart\/python-race-condition-example\" rel=\"noopener noreferrer nofollow\">GitHub<\/a>.<\/p>\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, \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u0447\u0442\u043e \u0432\u0430\u043c \u0431\u044b\u043b\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e!<\/p>\n<p>\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/docs.python.org\/3\/faq\/library.html#what-kinds-of-global-value-mutation-are-thread-safe\" rel=\"noopener noreferrer nofollow\">What kinds of global value mutation are thread-safe?<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/superfastpython.com\/context-switch-interval-in-python\/\" rel=\"noopener noreferrer nofollow\">Context Switch Interval In Python<\/a><\/p>\n<\/li>\n<\/ul>\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\/764420\/\"> https:\/\/habr.com\/ru\/articles\/764420\/<\/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-356257","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/356257","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=356257"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/356257\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=356257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=356257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=356257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}