NSThread 或 pythons' pyobjc 中的线程模块?

发布于 2024-07-14 02:44:19 字数 159 浏览 6 评论 0原文

我需要执行一些网络绑定调用(例如,获取网站),并且我不希望它阻止 UI。 如果我在 pyobjc 中工作,我应该使用 NSThread 还是 python 的线程模块? 我找不到任何有关如何选择其中之一的信息。 请注意,我并不真正关心 Python 的 GIL,因为我的任务根本不受 CPU 限制。

I need to do some network bound calls (e.g., fetch a website) and I don't want it to block the UI. Should I be using NSThread's or python's threading module if I am working in pyobjc? I can't find any information on how to choose one over the other. Note, I don't really care about Python's GIL since my tasks are not CPU bound at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

残疾 2024-07-21 02:44:19

这没有什么区别,您将通过略有不同的界面获得相同的行为。 使用最适合您系统的选项。

It will make no difference, you will gain the same behavior with slightly different interfaces. Use whichever fits best into your system.

不醒的梦 2024-07-21 02:44:19

学会爱上运行循环。 使用 Cocoa 的 URL 加载系统(或者,如果您需要普通套接字, NSFileHandle)并让它调用当响应(或失败)返回时您。 那么您根本不必处理线程(URL 加载系统将为您使用线程)。

几乎在 Cocoa 中创建自己的线程的唯一时间是当您有一个无法分解的大型任务(>0.1 秒)时。

(有人可能会说 NSOperation,但是 NSOperationQueue 已损坏 并且RAOperationQueue不支持并发操作。如果你已经有一堆 NSOperationQueue 代码或者真的想为工作 NSOperationQueue 做准备,但如果你现在需要并发操作,请运行循环或线程。)

Learn to love the run loop. Use Cocoa's URL-loading system (or, if you need plain sockets, NSFileHandle) and let it call you when the response (or failure) comes back. Then you don't have to deal with threads at all (the URL-loading system will use a thread for you).

Pretty much the only time to create your own threads in Cocoa is when you have a large task (>0.1 sec) that you can't break up.

(Someone might say NSOperation, but NSOperationQueue is broken and RAOperationQueue doesn't support concurrent operations. Fine if you already have a bunch of NSOperationQueue code or really want to prepare for working NSOperationQueue, but if you need concurrency now, run loop or threads.)

音盲 2024-07-21 02:44:19

我更喜欢原生 python 线程解决方案,因为我可以加入和引用周围的线程。 AFAIK,NSThread 不支持线程加入和取消,你可以使用 python 线程完成各种事情。

另外,令人遗憾的是 NSThread 不能有多个参数,尽管有解决方法(例如使用 NSDictionaryNSArray s),它仍然不像调用一个带有按顺序/相应参数排列的参数的线程那样优雅和简单。

但是,是的,如果情况要求您使用 NSThread,那么根本不应该有任何问题。 否则,坚持使用原生 python 线程是很酷的。

I'm more fond of the native python threading solution since I could join and reference threads around. AFAIK, NSThreads don't support thread joining and cancelling, and you could get a variety of things done with python threads.

Also, it's a bummer that NSThreads can't have multiple arguments, and though there are workarounds for this (like using NSDictionarys and NSArrays), it's still not as elegant and as simple as invoking a thread with arguments laid out in order / corresponding parameters.

But yeah, if the situation demands you to use NSThreads, there shouldn't be any problem at all. Otherwise, it's cool to stick with native python threads.

烟酒忠诚 2024-07-21 02:44:19

我有一个不同的建议,主要是因为Python线程由于GIL(全局解释器锁)而变得非常糟糕,特别是当你有多个CPU核心时。 有一个视频演示详细介绍了这一点,但我现在找不到该视频 - 它是由 Google 员工完成的。

不管怎样,你可能想考虑使用 subprocess 模块而不是线程(有一个可以执行的帮助程序,或者使用系统上的另一个二进制文件。或者使用 NSThread,它应该为你提供比使用 CPython 更高的性能线程。

I have a different suggestion, mainly because python threading is just plain awful because of the GIL (Global Interpreter Lock), especially when you have more than one cpu core. There is a video presentation that goes into this in excruciating detail, but I cannot find the video right now - it was done by a Google employee.

Anyway, you may want to think about using the subprocess module instead of threading (have a helper program that you can execute, or use another binary on the system. Or use NSThread, it should give you more performance than what you can get with CPython threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文