简体   繁体   English

jython 多线程

[英]jython multithreading

I started studying python a couple of month ago, then I found Jython.几个月前我开始学习 Python,然后我找到了 Jython。

Do threads work properly in Jython, since it doesn't have a GIL ?线程在 Jython 中是否正常工作,因为它没有 GIL If so, can you suggest a good book on concurrency (threading)?如果是这样,你能推荐一本关于并发(线程)的好书吗?

The best book I've encountered on multithreading is "Java Concurrency in Practice".我在多线程方面遇到的最好的书是“Java Concurrency in Practice”。 It's very much concentrating on Java thread concurrency, and is both humbling and exciting when you start to understand the problems and the possibilities introduced by concurrency.它非常专注于 Java 线程并发性,当您开始了解并发性引入的问题和可能性时,它既令人谦卑又令人兴奋。 The copy I bought a few years ago had some errata in the coding, though, which exacerbated an already brain-challenging subject: check out errata here: http://jcip.net/errata.html .不过,我几年前购买的副本在编码方面有一些勘误,这加剧了一个已经具有挑战性的主题:在此处查看勘误: http : //jcip.net/errata.html

Although designed for Java developers wishing to venture into concurrency (which by the way includes anyone who's ever used a GUI interface of any kind), I'm sure the technical difficulties and subtleties outlined in the book apply to any implementation of concurrency.虽然是为希望探索并发性的 Java 开发人员(顺便说一下,包括曾经使用过任何类型的 GUI 界面的任何人)而设计的,但我确信本书中概述的技术困难和微妙之处适用于任何并发性实现。

By the way, I also love Jython and can confirm that anything concurrency-wise that you can do in Java you can apparently do in Jython.顺便说一句,我也喜欢 Jython,并且可以确认任何可以在 Java 中执行的并发方面的操作显然都可以在 Jython 中执行。 However, there is a caveat: concurrency can be for asynchronous programming (including GUI) and/or for performance.但是,有一个警告:并发可以用于异步编程(包括 GUI)和/或性能。 If for the latter you have a problem, in my opinion: Jython in my experience runs about 10 x slower than the equivalent Java program.如果对于后者,您有问题,在我看来:根据我的经验,Jython 的运行速度比等效的 Java 程序慢 10 倍。

What this means is that your more demanding Jython modules will have to call something other than Jython for the number-crunching tasks.这意味着您要求更高的 Jython 模块将不得不调用 Jython 以外的其他东西来处理数字运算任务。 At the same time, Jython up to now* has not had Python's multiprocessing module, so inter-process communications are out, unless you venture into the dreaded territory of RMI.同时,Jython 到目前为止* 还没有 Python 的多处理模块,因此进程间通信已经过时,除非您冒险进入 RMI 的可怕领域。 You're more of a man/woman than I if you take that option.如果你选择那个选项,你比我更像一个男人/女人。 But everything's OK: please refer to "The Definitive Guide to Jython" at http://www.jython.org ... chapter 19 is a sort of whistle-stop intro to concurrency, and chapter 10 is about integrating Java and Jython (hint: it's absurdly easy).但一切都好:请参阅http://www.jython.org 上的“Jython 权威指南”……第 19 章是对并发的简要介绍,第 10 章是关于集成 Java 和 Jython(提示:这太容易了)。

  • interesting: a quick glimpse at the Jython site shows that, just 10 days ago, 17/05/12, version 2.7a1 was released... an "Alpha" release.有趣:快速浏览 Jython 站点表明,就在 10 天前,2012 年 5 月 17 日,发布了 2.7a1 版……一个“Alpha”版本。 This should contain the multiprocessing module, which came in with Python 2.6.这应该包含 Python 2.6 附带的多处理模块。 Wd be interesting to check this: if so it presumably gives you the exciting option of linking Jython and CPython processes (update later: sadly it appears for the moment that this is not so - the module name "multiprocessing" was not recognised when I tried)... Wd 检查这一点很有趣:如果是这样,它大概为您提供了链接 Jython 和 CPython 进程的令人兴奋的选项(稍后更新:可悲的是,目前看来并非如此 - 我尝试时无法识别模块名称“multiprocessing” )...

PS a final word: most experts who know much more about these things than I say that Moore's law is being superseded in importtance by Amdahl's law, which in short means that the daunting challenge of programming stable and scalable true concurrent programs will be unavoidable in the future. PS 最后一句话:大多数对这些事情了解的比我说的要多得多的专家说,摩尔定律的重要性正在被阿姆达尔定律所取代,简而言之,这意味着编写稳定和可扩展的真正并发程序的艰巨挑战在未来。 Exactly how easy true (ie thread) concurrency can be made with the use of clever code analysis tools I can't say but investment in this subject and the fascinating, intellectual new disciplines of reasoning imposed by concurrency will probably pay off... if you like a challenge.使用聪明的代码分析工具可以实现真正的(即线程)并发到底是多么容易我不能说,但是在这个主题上的投资以及并发强加的引人入胜的、智力新的推理学科可能会得到回报......如果你喜欢挑战。

Yes, with Jython you've real multi-threading .是的,使用 Jython 可以实现真正的多线程 Jython (JPython successor's) is an implementation of Python that runs in the JVM. Jython(JPython 的继承者)是在 JVM 中运行的 Python 实现。 One of the main differences between Jython and the original project is that the first doesn't have the GIL and implements a real multi-threading support based on the JVM's implementation. Jython 与原始项目的主要区别之一是,第一个项目没有 GIL,而是基于 JVM 的实现实现了真正的多线程支持。

I'd suggest you to take a look to this book and the OReilly's one.我建议你看看这本书OReilly 的本书

I have tried it with an example.我用一个例子试过了。

Requirements: 要求:
from rough import print_time from datetime import datetime """ This is actually using python threading package. One thing I came to know is that if we use the python threading module also, internally Jython chnages it to Java thread and work. """ # from threading import Thread, InterruptedException """ This is the java threading module. """ from java.lang import Thread, InterruptedException """ Here you can call your module from the run method. For passing arguments, you can use the constructor of the Cycle class. """ class Cycle(Thread): def __init__(self,time1=1): Thread.__init__(self) # arguments for the run method self.time1 = time1 def run(self): try: # Calling the required module with given arguments print_time(self.time1) except InterruptedException: print("Exception") if __name__ == '__main__': print("start time:",datetime.now()) for i in range(100): Cycle(i).start() print("end time:",datetime.now())

Please find the full code in https://github.com/om12nayak/Jython_multithreading_demo请在https://github.com/om12nayak/Jython_multithreading_demo 中找到完整代码

The initially confusing aspect may be that you can mix and match Java and Jython's concurrency mechanisms.最初令人困惑的方面可能是您可以混合搭配Java 和 Jython 的并发机制。 But it all seems to work.但这一切似乎都奏效了。 The reasons are:原因是:

  • Underneath Jython is the same old Java. Jython 下面是同样的旧 Java。 All of its robust threading mechanisms and data structures aren't going to break even under the heavy Jython machinery.在繁重的 Jython 机器下,其所有强大的线程机制和数据结构都不会收支平衡。
  • Jython's threads take Java threads as their chassis and add some superstructure to make them speak the Python threading API. Jython 的线程以 Java 线程为框架,并添加了一些上层结构,使它们能够使用 Python 线程 API。 (There is no better way to make porting threaded Python code easier.) But the essence of the two types of threads is similar. (没有更好的方法可以更轻松地移植线程化 Python 代码。)但这两种线程的本质是相似的。 Except that with Jython threads除了 Jython 线程

    ... there are no priorities, no thread groups, and threads cannot be destroyed, stopped, suspended, resumed, or interrupted. ...没有优先级,没有线程组,线程不能被破坏、停止、暂停、恢复或中断。 [1] [1]

The Python idioms are probably a bit more convenient, because, for example, if you wish to do the equivalent of synchronized (some_object) { ... } , there is a small bit of fiddling required , which is likely to be less readable than using anRLock . Python 习惯用法可能更方便一些,因为,例如,如果您希望执行与synchronized (some_object) { ... }等效的操作,则需要进行一些修改,这可能比使用RLock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM