哪些脚本语言支持多核编程?
我编写了一个小 python 应用程序,在这里您可以看到任务管理器在典型运行期间的外观。
weinzierl.name)
(来源: 完美的多线程,毫不奇怪,它只使用一个 CPU 核心。 尽管大多数现代脚本语言都支持多线程,但脚本只能在一个 CPU 核心上运行。
Ruby、Python、Lua、PHP 都只能在单核上运行。 就连据说特别适合并发编程的Erlang也受到了影响。
是否有内置的脚本语言 支持不限于单核的线程吗?
总结
答案并不完全符合我的预期,但是TCL
答案很接近。 我想添加 perl
,它(很像 TCL
)具有基于解释器的线程。
Jython、IronPython 和 Groovy 属于将经过验证的语言与经过验证的虚拟机相结合的保护伞另一种语言。 感谢您在这方面的提示 方向。
我选择Aiden Bell的答案作为已接受回答。 他没有建议某种特定的语言,但他的话对我来说是最有洞察力的。
I have written a little python application and here you can see how Task Manager looks during a typical run.
(source: weinzierl.name)
While the application is perfectly multithreaded, unsurprisingly it uses only one CPU core.
Regardless of the fact that most modern scripting languages support multithreading, scripts can run on one CPU core only.
Ruby, Python, Lua, PHP all can only run on a single core.
Even Erlang, which is said to be especially good for concurrent programming, is affected.
Is there a scripting language that has built in
support for threads that are not confined to a single core?
WRAP UP
Answers were not quite what I expected, but the TCL
answer comes close.
I'd like to add perl
, which (much like TCL
) has interpreter-based threads.
Jython, IronPython and Groovy fall under the umbrella of combining a proven language with the proven virtual machine of another language. Thanks for your hints in this
direction.
I chose Aiden Bell's answer as Accepted Answer.
He does not suggest a particular language but his remark was most insightful to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您似乎使用的“脚本语言”定义可能会引起一些人的注意,我不知道这对您的其他要求意味着什么。
无论如何,你考虑过TCL吗? 我相信它会做你想做的事。
由于您在列表中包含了相当通用的语言,因此我不知道您可以接受多繁重的实现。 如果无数的Scheme实现之一不支持本机线程,我会感到惊讶,但在我的脑海中,我只记得曾经使用的MzScheme,但我似乎记得支持已被放弃。 当然,一些 Common LISP 实现在这方面做得很好。 如果 Embeddable Common Lisp (ECL) 可以,它可能适合您。 但我不使用它,所以我不确定它的线程支持状态如何,这当然可能取决于平台。
更新 另外,如果我没记错的话,GHC Haskell 并没有完全按照你的要求去做,但可能会有效地做你想要的事情,因为,正如我记得的,它会为每个线程旋转一个本机线程core 左右,然后在这些上运行它的线程......
You seem use a definition of "scripting language" that may raise a few eyebrows, and I don't know what that implies about your other requirements.
Anyway, have you considered TCL? It will do what you want, I believe.
Since you are including fairly general purpose languages in your list, I don't know how heavy an implementation is acceptable to you. I'd be surprised if one of the zillion Scheme implementations doesn't to native threads, but off the top of my head, I can only remember the MzScheme used to but I seem to remember support was dropped. Certainly some of the Common LISP implementations do this well. If Embeddable Common Lisp (ECL) does, it might work for you. I don't use it though so I'm not sure what the state of it's threading support is, and this may of course depend on platform.
Update Also, if I recall correctly, GHC Haskell doesn't do quite what you are asking, but may do effectively what you want since, again, as I recall, it will spin of a native thread per core or so and then run its threads across those....
您可以在 Jython(在 JVM 上,就像 @Reginaldo 提到的 Groovy 一样)和 IronPython(在 .NET 上)等实现中使用 Python 语言自由地实现多线程。 对于 Python 语言的经典 CPython 实现,正如 @Dan 的评论提到的,
多处理
(而不是线程
)是自由使用尽可能多的可用内核的方法You can freely multi-thread with the Python language in implementations such as Jython (on the JVM, as @Reginaldo mention Groovy is) and IronPython (on .NET). For the classical CPython implementation of the Python language, as @Dan's comment mentions,
multiprocessing
(rather thanthreading
) is the way to freely use as many cores as you have available线程语法可能是静态的,但跨操作系统和虚拟机的实现可能会改变
您的脚本语言可能在一个操作系统上使用真正的线程,而在另一个操作系统上使用假线程。
如果您有性能要求,可能值得考虑确保脚本化线程落入操作系统中最有利的层。 用户空间线程会更快,但对于主要阻塞的线程活动,内核线程会更好。
Thread syntax may be static, but implementation across operating systems and virtual machines may change
Your scripting language may use true threading on one OS and fake-threads on another.
If you have performance requirements, it might be worth looking to ensure that the scripted threads fall through to the most beneficial layer in the OS. Userspace threads will be faster, but for largely blocking thread activity kernel threads will be better.
由于 Groovy 基于 Java 虚拟机,因此您可以获得对真正线程的支持。
As Groovy is based on the Java virtual machine, you get support for true threads.
.NET 4 上的 F# 对并行编程具有出色的支持和极好的性能,并且支持专为脚本编写而设计的 .fsx 文件。 我使用 F# 编写所有脚本。
F# on .NET 4 has excellent support for parallel programming and extremely good performance as well as support for .fsx files that are specifically designed for scripting. I do all my scripting using F#.
这个问题的答案已经被接受,但要补充的是,除了 tcl 之外,我所知道的唯一支持多线程和线程安全编程的解释脚本语言是 Qore。
Qore从下到上的设计就是为了支持多线程; 该语言的每个方面都是线程安全的; 该语言旨在原生支持 SMP 可扩展性和多线程。 例如,您可以使用
background
运算符启动新线程,或使用ThreadPool
类来管理线程池。 Qore 还会抛出常见线程错误的异常,以便程序员可以立即看到线程错误(例如潜在的死锁或线程 API 的错误,例如试图获取当前线程已持有的锁)。Qore还支持线程资源; 例如,
DatasourcePool
分配被视为线程本地资源; 如果您在结束线程之前忘记提交或回滚事务,DatasourcePool
类的线程资源处理将自动回滚事务并抛出异常,并提供有关问题的用户友好信息,以及它是如何解决的。也许它对您有用 - Qore 功能的概述如下:为什么使用 Qore?。
An answer for this question has already been accepted, but just to add that besides tcl, the only other interpreted scripting language that I know of that supports multithreading and thread-safe programming is Qore.
Qore was designed from the bottom up to support multithreading; every aspect of the language is thread-safe; the language was designed to support SMP scalability and multithreading natively. For example, you can use the
background
operator to start a new thread or theThreadPool
class to manage a pool of threads. Qore will also throw exceptions with common thread errors so that threading errors (like potential deadlocks or errors with threading APIs like trying to grab a lock that's already held by the current thread) are immediately visible to the programmer.Qore additionally supports and thread resources; for example, a
DatasourcePool
allocation is treated as a thread-local resource; if you forget to commit or roll back a transaction before you end your thread, the thread resource handling for theDatasourcePool
class will roll back the transaction automatically and throw an exception with user-friendly information about the problem and how it was solved.Maybe it could be useful for you - an overview of Qore's features is here: Why use Qore?.
CSScript 与 并行扩展不应该是一个糟糕的选择。 您用纯 C# 编写代码,然后将其作为脚本运行。
CSScript in combination with Parallel Extensions shouldn't be a bad option. You write your code in pure C# and then run it as a script.
它与线程机制无关。 问题是(例如在 python 中)你必须获得解释器实例才能运行脚本。 要获取解释器,您必须锁定它,因为它将保留引用计数等,并且需要避免并发访问该对象。 Python 使用 pthread,它们是真正的线程,但是当您使用 python 对象时,只有一个线程正在运行,其他线程正在等待。 他们称之为 GIL(全局解释器锁),这是导致进程内不可能实现真正并行性的主要问题。
https://wiki.python.org/moin/GlobalInterpreterLock
其他脚本语言可能有某种一样的问题。
It is not related to the threading mechanism. The problem is that (for example in python) you have to get interpreter instance to run the script. To acquire the interpreter you have to lock it as it is going to keep the reference count and etc and need to avoid concurrent access to this objects. Python uses pthread and they are real threads but when you are working with python objects just one thread is running an others waiting. They call this GIL (Global Interpreter Lock) and it is the main problem that makes real parallelism impossible inside a process.
https://wiki.python.org/moin/GlobalInterpreterLock
The other scripting languages may have kind of the same problem.
Guile 支持 POSIX 线程,我认为这是硬件线程。
Guile supports POSIX threads which I believe are hardware threads.