简体   繁体   English

如何在 Python 下为 Delphi 创建线程

[英]How create threads under Python for Delphi

I'm hosting Python script with Python for Delphi components inside my Delphi application.我在 Delphi 应用程序中使用 Python for Delphi 组件托管 Python 脚本。 I'd like to create background tasks which keep running by script.我想创建由脚本继续运行的后台任务。

Is it possible to create threads which keep running even if the script execution ends (but not the host process, which keeps going on).是否可以创建即使脚本执行结束也继续运行的线程(但不是主机进程,它继续运行)。 I've noticed that the program gets stuck if the executing script ends and there is thread running.我注意到如果执行脚本结束并且线程正在运行,程序就会卡住。 However if I'll wait until the thread is finished everything goes fine.但是,如果我等到线程完成,一切都会好起来。

I'm trying to use "threading" standard module for threads.我正在尝试对线程使用“线程”标准模块。

Python has its own threading module that comes standard, if it helps.如果有帮助,Python 有自己的标准线程模块。 You can create thread objects using the threading module.您可以使用 threading 模块创建线程对象。

threading Documentation线程文档

thread Documentation线程文档

The thread module offers low level threading and synchronization using simple Lock objects. thread 模块使用简单的 Lock 对象提供低级线程和同步。

Again, not sure if this helps since you're using Python under a Delphi environment.同样,不确定这是否有帮助,因为您在 Delphi 环境下使用 Python。

Threads by definition are part of the same process.根据定义,线程是同一进程的一部分。 If you want them to keep running, they need to be forked off into a new process;如果您希望它们继续运行,则需要将它们分叉到一个新进程中; see os.fork() and friends.见 os.fork() 和朋友。

You'll probably want the new process to end (via exit() or the like) immediately after spawning the script.您可能希望新进程在生成脚本后立即结束(通过 exit() 等)。

If a process dies all it's threads die with it, so a solution might be a separate process.如果一个进程死掉了它的所有线程,那么解决方案可能是一个单独的进程。

See if creating a xmlrpc server might help you, that is a simple solution for interprocess communication.看看创建一个 xmlrpc 服务器是否对你有帮助,这是一个简单的进程间通信解决方案。

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

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