使用spawn/threads的基本Python服务器
我遇到了一个问题。这应该很简单。
我有一个应用程序将数据放入目录“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Python 中,您应该考虑使用 multiprocessing 模块而不是线程,特别是如果您有多核机器:
另请参阅以下示例和介绍。
In Python, you should consider using the multiprocessing module instead of threads, especially if you have a multicore machine:
Also consult the following for examples and an introduction.