对于一些简单的线程相关代码,即:
\nimport threading\n\n\na = 0\nthreads = []\n\n\ndef x():\n global a\n for i in range(1_000_000):\n a += 1\n\n\nfor _ in range(10):\n thread = threading.Thread(target=x)\n threads.append(thread)\n thread.start()\n\n\nfor thread in threads:\n thread.join()\n\n\nprint(a)\nassert a == 10_000_000\nRun Code Online (Sandbox Code Playgroud)\n根据 Python 版本,我们得到了不同的行为。
\n对于 3.10,输出为:
\n\xe2\x9d\xaf python3.10 b.py\n10000000\nRun Code Online (Sandbox Code Playgroud)\n对于 3.9,输出为:
\n\xe2\x9d\xaf python3.9 b.py\n2440951\nTraceback (most recent call last):\n File "/Users/romka/t/threads-test/b.py", line 24, in <module>\n assert a == 10_000_000\nAssertionError\nRun Code Online (Sandbox Code Playgroud)\n由于我们没有获取任何锁,对我来说,3.9 的结果是显而易见的并且是预期的。问题是为什么 3.10 得到了“正确”的结果,而不应该得到“正确”的结果?
\n我正在查看 Python 3.10 的变更日志,没有任何与线程或 GIL 相关的内容可以带来这样的结果。
\npython multithreading python-multithreading python-internals
当您为应用程序设计 URL 模式时,您使用哪些规则?
我更喜欢:/post/用于列表,/post/23/用于详细信息,/post/23/edit/用于编辑,只是因为我可以在浏览器位置栏中手动轻松使用该 URL。我错了?请推荐我。
谢谢。
在DB中我有表(User),它为该用户存储时区(作为字符串值,例如:"Europe/Oslo").现在,我需要获得所有用户,现在当地时间为例如:上午9点.
有没有什么好方法可以做到这一点,没有对所有用户进行循环?如果pytz能够返回时区列表,那么现在时间是9AM,我可以使用简单的IN SQL语句.