线程在Python中如何工作,什么是常见的Python线程特定陷阱?(How do threads work in Python, and what are common Python-threading specific pitfalls?)
我一直在试图围绕Python中的线程工作,很难找到有关它们如何运行的良好信息。 我可能只是错过了一个链接或某些东西,但是看起来官方的文档在这个问题上并不十分彻底,而且我还没有找到一个很好的写作。
从我可以看出,只有一个线程可以一次运行,并且活动线程每10个指令切换一次?
哪里有很好的解释,还是可以提供一个? 要知道使用线程使用Python时遇到的常见问题,这也是非常好的。
I've been trying to wrap my head around how threads work in Python, and it's hard to find good information on how they operate. I may just be missing a link or something, but it seems like the official documentation isn't very thorough on the subject, and I haven't been able to find a good write-up.
From what I can tell, only one thread can be running at once, and the active thread switches every 10 instructions or so?
Where is there a good explanation, or can you provide one? It would also be very nice to be aware of common problems that you run into while using threads with Python.
原文:https://stackoverflow.com/questions/31340
最满意答案
整数除法总是导致一个整数; 所以:
word.Length / 2
返回int
(它向下word.Length / 2
)。当你对此调用
Math.Ceiling
时,你传递了一个整数,但是没有Math.Ceiling(int)
。 它有两个选择:Math.Ceiling(double)
和Math.Ceiling(decimal)
,但是:它也可以使用,而且从编译器的角度来看,这两种选择都不会更好。坦率地说,使用通用的“页数”公式可能会更简单:
int pages = (items + pageSize - 1) / pageSize;
在这种情况下变得简单:
int upperLimit = (word.Length + 1) / 2;
(请注意,通用页面计数公式也可以写成
int pages = ((items - 1) / pageSize) + 1;
但在这种情况下,替换固定页面大小将更困难)Integer division always results in an integer; so:
word.Length / 2
returnsint
(it rounds down).When you call
Math.Ceiling
on this, you are passing an integer, but there is notMath.Ceiling(int)
. It has two choices:Math.Ceiling(double)
andMath.Ceiling(decimal)
, but: it could use either, and neither of those is better from the compiler's perspective.Frankly, it might be simpler to use the general purpose "page count" formula:
int pages = (items + pageSize - 1) / pageSize;
which in this case becomes simply:
int upperLimit = (word.Length + 1) / 2;
(note that the general purpose page count formula can also be written
int pages = ((items - 1) / pageSize) + 1;
, although in this case it would be harder to substitute your fixed page size)
相关问答
更多-
您正在使用d3.tsv ,您应该使用d3.csv 。 You are using d3.tsv, you should be using d3.csv instead.
-
整数除法总是导致一个整数; 所以: word.Length / 2返回int (它向下word.Length / 2 )。 当你对此调用Math.Ceiling时,你传递了一个整数,但是没有 Math.Ceiling(int) 。 它有两个选择: Math.Ceiling(double)和Math.Ceiling(decimal) ,但是:它也可以使用,而且从编译器的角度来看,这两种选择都不会更好。 坦率地说,使用通用的“页数”公式可能会更简单: int pages = (items + pageSize ...
-
因此,我决定进行测试,结果有些不确定,但很明显,性能上的任何差异都是微不足道的。 这是我编写的代码来测试它: var times = 100000; string s = "abc"; Stopwatch sw = new Stopwatch(); sw.Start(); for (var i = 0; i < times; i++) { bool notEmpty1 = s.Length != 0; } var diff = ...
-
括号覆盖正常的运算符优先级。 特别是函数和参数具有非常高的优先级,因此在后一种情况下,它被评估为 (String.length "hello") + " " + "world!" 然后尝试在字符串中添加一个数字。 The parentheses are overriding the normal operator precedence. In particular function & argument has a very high precedence, so in the latter case i ...
-
非可调用成员'String.Length'不能像方法一样使用(Non-Invocable Member 'String.Length' cannot be used like a method)[2023-05-31]
Length是类型字符串的属性,因此删除paranthesis。 只需致电: suffix.Length; 也 for(int i = compare_length; i < length_full - 1; i++) { //something } 因为你必须在循环中将i定义为int。 加, lastName = (String)entire_name[i]; 是错的 使用 : lastName = entire_name[i].ToString(); the Length is a pro ... -
你必须施放第二个参数: newTransform("foo", (XMLStructure) null) 您收到模糊警告,因为null是两个方法的有效参数。 您需要添加(XMLStructure)以告诉编译器您要设置为null的对象的类型。 You must cast the second argument: newTransform("foo", (XMLStructure) null) You are getting the ambiguous warning because null is a v ...
-
MVC - 以下方法或属性之间的调用不明确(MVC - The call is ambiguous between the following methods or properties)[2023-03-24]
我猜你已将你的Extensions类文件放在App_Code文件夹中,这是正确的吗? 如果是这样,将其移到App_Code之外并删除该文件夹。 App_Code在MVC应用程序中没有位置,默认情况下,它们创建为Web应用程序项目而不是Web站点项目。 Web应用程序在构建时编译为dll,App_Code中的任何内容也在运行时编译为单独的dll。 因此存在两种方法和模糊性。 I'm guessing that you have put your Extensions class file in an App_ ... -
这是因为.length() (和.size() )返回size_t ,这是一个unsigned int。 你认为你得到一个负数,实际上它下溢回size_t的最大值(在我的机器上,这是18446744073709551615)。 这意味着你的for循环将循环遍历size_t的所有可能值,而不是像你期望的那样立即退出。 要获得所需的结果,可以显式地将大小转换为int ,而不是unsigned int (参见aslgs answer),尽管对于具有足够长度的字符串可能会失败(足够超过/低于标准int ) 编辑:以 ...
-
String.length是一个函数,但是由于Elixir允许调用没有括号的函数,所以在键入String.length时实际上并没有返回函数,而是使用0个参数调用该函数的结果。 你将不得不使用&module.function/arity语法: iex(1)> Enum.map(["the", "huge", "elephant"], &String.length/1) [3, 4, 8] String.length is a function but as Elixir allows calling a ...
-
你可以试试: return (int)Math.Ceiling((double)( (double)_BytesReceived / (double)_TotalBytesToReceive * 100.0d )); 或者如果您更喜欢float : return (int)Math.Ceiling((float)( (float)BytesReceived / (float)_TotalBytesToReceive * 100.0f )); 原因是,Math.Ceil超载,接受float和d ...