简体   繁体   English

ASP.NET MVC中的后台线程“作业队列”

[英]Background thread 'job-queue' in ASP.NET MVC

Context: I have a photo-uploading website set up. 上下文:我已经建立了一个上传照片的网站。 I need to perform operations on these uploads every time a user uploads a photo. 每次用户上传照片时,我都需要对这些上传执行操作。

With help from other users here on SO, I came to the conclusion, that I needed a background thread that accepted these "processing jobs", so I could return response to the user quickly, and let the background thread work on these background jobs. 在SO上其他用户的帮助下,我得出的结论是,我需要一个接受这些“处理作业”的后台线程,因此我可以快速将响应返回给用户,并让后台线程在这些后台作业上工作。

I'm sort of "set" on a threading solution as opposed to a service for instance, as it's not possible for me to set up a service on the webserver. 与某种服务相比,我有点“设置”在线程解决方案上,因为我无法在Web服务器上设置服务。 I've read some things on message queues, and background threads, but I'm really in need of is some practical pointers as to how I should proceed. 我已经阅读了一些有关消息队列和后台线程的内容,但是我真的需要一些有关如何进行的实用指针。

Also - are there any things I should be aware of? 另外-有什么我应该注意的事情吗? Off the top of my head, I'm thinking about the number of threads, and possibly hitting a snag with the IIS or server if too many threads are running? 我在想什么,我正在考虑线程的数量,如果正在运行太多的线程,是否有可能与IIS或服务器发生冲突? That's why I'm thinking it should be a single background thread per user, and not a thread per job, as there could be MANY photos uploaded at once. 这就是为什么我认为它应该是每个用户一个后台线程,而不是每个作业一个线程,因为一次可以上传很多照片。 So a single thread per user that takes care of the jobs in a 'queue' like fashion. 因此,每个用户有一个线程以“队列”之类的方式处理作业。 Am I way off base? 我离基地远吗?

You can run as many threads as you like, but you'll run the risk of spending more time on context switching that actual crunching. 您可以根据需要运行任意数量的线程,但是会冒着花更多时间在上下文切换上进行实际操作的风险。 If you need good CPU performance you should use no more than 1 thread per CPU core. 如果您需要良好的CPU性能,则每个CPU内核最多​​只能使用1个线程。 The PLINQ stuff has this exact strategy. PLINQ的东西具有这种精确的策略。 If you tell PLINQ to run a query it will execute in parallel the equivalent of the number of CPU cores available on your system. 如果您告诉PLINQ运行查询,它将并行执行与系统上可用CPU核心数量相等的查询。

If you're gonna implement queue, you should be thinking of a FIFO queue, users put their work in a bunch of threads or servers pull work from this queue and does the work. 如果要实现队列,您应该考虑一个FIFO队列,用户将其工作放在一堆线程中,或者服务器从该队列中提取工作并完成工作。

ie you can use a SQL Server database to synchronize work cross many machines using a FIFO queue in the database (this is just a simple table that you pull work from). 也就是说,您可以使用SQL Server数据库通过数据库中的FIFO队列在许多计算机之间同步工作(这只是从中提取工作的简单表)。 It will scale pretty well and it's also more robust because work can be resumed if it crashes or timeouts. 它可以很好地扩展,并且也更强大,因为如果崩溃或超时,可以恢复工作。

You should read this question i posted about this a while back. 您应该阅读我不久前发布的有关此问题的信息。 Remus Rusanu posted some intresting links on the topic that discusses the use of a database to orchestrate work loads. Remus Rusanu在该主题上发布了一些引人入胜的链接,其中讨论了如何使用数据库来编排工作负载。

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

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