在 python Paste 中启动我自己的线程

发布于 2024-08-08 10:54:48 字数 204 浏览 1 评论 0原文

我正在使用塔和粘贴编写一个网络应用程序。在 HTTP 请求完成后,我想做一些工作(发送一些电子邮件、向数据库写入一些内容等),但我不想阻止 HTTP 请求。

如果我启动一个线程来完成这项工作,可以吗?我总是看到有关粘贴杀死挂起的线程等的内容。它会杀死我正在工作的线程吗?

我在这里还能做什么?有没有办法让请求返回,但在完成后运行一些代码?

谢谢。

I'm writing a web application using pylons and paste. I have some work I want to do after an HTTP request is finished (send some emails, write some stuff to the db, etc) that I don't want to block the HTTP request on.

If I start a thread to do this work, is that OK? I always see this stuff about paste killing off hung threads, etc. Will it kill my threads which are doing work?

What else can I do here? Is there a way I can make the request return but have some code run after it's done?

Thanks.

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

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

发布评论

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

评论(4

愛放△進行李 2024-08-15 10:54:48

您可以使用线程方法(也许设置 Thead.daemon 财产会有所帮助——但我不确定)。

但是,我建议研究任务排队系统。您可以将任务放入队列中(速度非常快),然后侦听器可以异步处理任务,从而允许 HTTP 请求快速返回。我知道 Django 有两个任务队列:

您还可以考虑使用更“企业”的消息传递解决方案,例如作为 RabbitMQActiveMQ

编辑上一个答案 有一些好的指示。

You could use a thread approach (maybe setting the Thead.daemon property would help--but I'm not sure).

However, I would suggest looking into a task queuing system. You can place a task on a queue (which is very fast), then a listener can handle the tasks asynchronously, allowing the HTTP request to return quickly. There are two task queues that I know of for Django:

You could also consider using an more "enterprise" messaging solution, such as RabbitMQ or ActiveMQ.

Edit: previous answer with some good pointers.

卖梦商人 2024-08-15 10:54:48

我认为最好的解决方案是消息传递系统,因为它可以配置为在 pylons 进程出现故障时不会丢失任务。我总是会使用进程而不是线程,尤其是在这种情况下。如果您使用的是 python 2.6+,请使用内置的 multiprocessing 或者您可以随时安装您可以在 pypi 上找到处理模块(我无法发布链接,因为我是新用户)。

I think the best solution is messaging system because it can be configured to not loose the task if the pylons process goes down. I would always use processes over threads especially in this case. If you are using python 2.6+ use the built in multiprocessing or you can always install the processing module which you can find on pypi (I can't post link because of I am a new user).

自由如风 2024-08-15 10:54:48

看看 gearman,它是专门为将任务分包给“工人”来处理而设计的。他们甚至可以用完全不同的语言来处理它。你可以回来询问任务是否完成,或者就让它完成。这对于许多任务来说应该很有效。

如果您绝对需要确保它已完成,我建议在数据库或持久性的地方对任务进行排队,然后有一个单独的进程来运行它,以确保每个任务都得到适当的处理。

Take a look at gearman, it was specifically made for farming out tasks to 'workers' to handle. They can even handle it in a different language entirely. You can come back and ask if the task was completed, or just let it complete. That should work well for many tasks.

If you absolutely need to ensure it was completed, I'd suggest queuing tasks in a database or somewhere persistent, then have a separate process that runs through it ensuring each one gets handled appropriately.

德意的啸 2024-08-15 10:54:48

要直接回答您的基本问题,您应该能够按照您的意愿使用线程。 “杀死挂起的线程”部分是粘贴清理它自己的线程,而不是你的线程。

还有其他软件包可能会有所帮助,等等,但我建议您从简单的线程开始,看看您能走多远。只有这样你才会知道接下来需要什么。

(请注意,“Thread.daemon”在这里应该与您无关。设置为 true 将确保您启动的线程不会阻止整个进程退出。但是,这样做意味着如果进程“干净地”退出( (而不是被迫退出)即使没有完成工作,您的线程也会被终止。这是否是一个问题,以及您如何处理类似的事情,完全取决于您自己的要求和设计。

To answer your basic question directly, you should be able to use threads just as you'd like. The "killing hung threads" part is paste cleaning up its own threads, not yours.

There are other packages that might help, etc, but I'd suggest you start with simple threads and see how far you get. Only then will you know what you need next.

(Note, "Thread.daemon" should be mostly irrelevant to you here. Setting that true will ensure a thread you start will not prevent the entire process from exiting. Doing so would mean, however, that if the process exited "cleanly" (as opposed to being forced to exit) your thread would be terminated even if it wasn't done its work. Whether that's a problem, and how you handle things like that, depend entirely on your own requirements and design.

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