使用spawn/threads的基本Python服务器

发布于 2024-09-11 12:54:23 字数 410 浏览 4 评论 0原文

我遇到了一个问题。这应该很简单。

我有一个应用程序将数据放入目录“A”中。数据将是一系列文件。

我想要一个持续运行的服务器,它会持续查看目录,并且在看到目录中的完整文件时,服务器会生成/分叉/创建一个线程(不确定python中的确切单词/技术)然后执行一些工作。

基本上,我将在线程中执行外部文件的 include/execfile("foo") 操作,以根据目录“A”中的文件执行工作。

我希望能够同时运行多个线程。所以我希望尽可能快地运行整个进程,并且实现线程/生成/分叉进程应该允许我有多个线程并行运行。不同的工作流程之间没有沟通。

我见过各种使用扭曲等的例子..但我想我想太多了..

我可以玩的任何简单/完整的例子都会很棒! (指向“网上样本”的指针也很酷......

谢谢......

Got a problem that I'm facing. and it should be pretty simple.

I have an app that places data into a dir "A". The data will be a series of files.

I want to have a continually running server, that does a continual look at the dir, and on seeing a completed file in the dir, the server spawns/forks/creates a thread (not sure of the exact word/tech in python) that then performs some work.

Basically, I'm going to be doing an include/execfile("foo") of an external file in the thread, to perform work, based on the file in the dir "A".

I want to be able to have the multiple threads running at the same time. So i'm looking to run the entire process as fast as possible, and implementing threads/spawn/forked process should allow me to have multiple threads running in parallel. There's no communication between the different work processes.

I've seen various examples using twisted, etc.. but I think I'm over thinking this..

Any simple/complete example that I can play with would be great!! (pointers to samples on the 'net would also be cool...

thanks...

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

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

发布评论

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

评论(1

爱的故事 2024-09-18 12:54:23

在 Python 中,您应该考虑使用 multiprocessing 模块而不是线程,特别是如果您有多核机器:

multiprocessing 是一个使用类似于线程模块的 API 支持生成进程的包。多处理包提供本地和远程并发,通过使用子进程而不是线程有效地回避全局解释器锁。因此,多处理模块允许程序员充分利用给定机器上的多个处理器。它可以在 Unix 和 Windows 上运行

另请参阅以下示例和介绍。

In Python, you should consider using the multiprocessing module instead of threads, especially if you have a multicore machine:

multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows

Also consult the following for examples and an introduction.

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