问题标签 [blockingcollection]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c# - 在 BlockingCollection 中插入元素
是否可以在 -not empty- BlockingCollection 中的第一个位置(或编号位置)插入一个元素?就像列表的“插入”方法一样。
就像是:
c# - C# BlockingCollection 生产者消费者不阻塞消费者线程
我有一种情况,我需要有大量(数百个)队列,其中项目应该按顺序处理(需要单线程消费者)。我的第一个实现基于示例,我为每个 BlockingCollection 使用了一个长时间运行的任务来使用队列项。然而,我最终得到了一个有数百个线程的应用程序,这些线程大多处于空闲状态,除了消耗内存之外什么都不做,因为队列大部分时间都是空的。
我认为只有在队列中有要处理的东西时才运行消费者任务会更好,但是,我无法找到提供最佳实践的示例。
我想出了一个类似于下面的解决方案。但问题是,每个项目都会导致一个新任务(也许这效率低下?浪费资源?)。但是,如果我不为每个项目创建一个新任务,我不能保证一个项目不会在未处理的队列中。
什么是这种情况的最佳实践/最佳解决方案,并保证不存在项目在队列中但没有消费者正在运行的情况?
c# - 正如我所料,这个 BlockingCollection 不是 FIFO
感谢您查看我的问题:我有一个(非 gui 线程)BlockingCollection
,我一直认为它是 FIFO(先进先出),但我现在意识到它不是
我在dotnetfiddle上粘贴了一个代码示例,但是因为它不运行多线程,所以你看不到错误发生,但你可以看到代码
好的。那我想要什么?我想在其中创建第二个线程(非 GUI),Visual Studio Express 2013 C# Winforms
就像一个工作室,它会按照发送它们的顺序执行它传递的事情。
我选择了这样的结构:
这种奇怪安排的原因是我想拥有多达 20 或 30types
个队列对象(我将这些都称为QDoType_something
)并且我对布局感到满意,但如果我调用引擎将无法工作
我明白了
或者
所以它显然不是“ FIFO
”,这是令人震惊的..有没有办法确保我BlockingCollection
是a)not gui
线程b)只作为一个额外的线程运行和c)第二个线程总是在运行FIFO
(先进先出?)
根据要求:这是正确的代码:
=QDoType_test.cs=
=nofQDo.cs=
=QDoType.cs=
=QDo.cs=
python - 可以在网络应用程序中使用 Pika BlockingConnection 吗?
BlockingConnection
我对and有点困惑AsyncoreConnection
。我想从 Django 应用程序向 RabbitMQ 队列发送一些消息。BlockingConnection
使用全局对象可以做到这一点吗?
谢谢你。
c# - 无法在第二轮中将项目添加到集合中
基本上我的Windows服务应用程序中有一个blockingcollection,每次我想向集合中添加4个项目然后处理它。
第一轮还好,第二轮就失败了。错误是
BlockingCollection 已被标记为关于添加的完整。
我的代码:
主要应用代码:
我意识到有一个代码bc.CompleteAdding();
可以阻止进一步的添加。所以我把它注释掉了,但它不会进入第二轮区块。它没有到达代码Console.WriteLine("Blocking end");
它与我的旧线程相同。
c# - 完成两个任务然后打印一些东西
我有三个任务,一个是生产者,然后是消费者,最后一个是在完成前两个任务后打印一些东西。但是代码没有到达最后一个任务,这意味着没有打印。
请看我的代码逻辑。
编辑:
对于“DoConsume”,它很复杂。
c# - 您可以将多个 BlockingCollections 包装在一个后备存储周围吗?
我想知道是否可以使用单个后备存储实例化多个 BlockingCollections,例如:
本次调查的动机如下;BlockingCollection
需要调用它的 Dispose 方法。这与使用using
块是无缝的。然而,由于一个using
块并不总是合适的(考虑生产者和消费者不仅生活在不同的方法中,而且还生活在不同的线程上)试图做调用 Dispose 所需的簿记变得不那么琐碎,而且更容易出错. 但是,如果不是传播 a BlockingCollection
,而是传播后备存储,则BlockingCollection
可以在using
块内本地实例化一个本地。
我的猜测是您可以,因为 IProducerConsumerCollection 接口中没有任何内容,这是 BlockingCollection 唯一依赖的东西,这似乎暗示了其他情况。
c# - Task.WhenAll 与 BlockingCollection 产生的无限任务的使用
我正在将后台任务添加到阻塞集合(在后台添加)。
我正在等待 GetConsumingEnumerable 返回的 Enumerable 上的 Task.WhenAll。
我的问题是:接收 IEnumerable 的 Task.WhenAll 的超载是否“准备好”接收无穷无尽的任务?
我只是不确定我是否可以这样做,或者它是否打算以这种方式使用?
c# - 使用任务和阻塞集合来完成无休止的任务
我正在寻找从线路中读取数据并将其发送到函数以解析内容。由于消息 (xml) 的大小可能不同,我可以阅读整条消息、多条消息或消息部分。
我正在尝试使用当我从网络上读取数据并使用消费者线程将数据拉出以进行解析时BlockingCollection
调用的地方来实现代码。这些示例看起来很简单,但它们似乎只工作一次,然后退出。我希望消费者在消息进入时不断解析。请参阅下面的代码了解我目前在做什么。TryAdd
BlockingCollection
消息处理:
读取电线(这在一个线程中运行):
那么谁能告诉我我在这里做错了什么?
c# - blocking collection process n items at a time - continuing as soon as 1 is done
I have the following Scenario.
I take 50 jobs from the database into a blocking collection.
Each job is a long running one. (potentially could be). So I want to run them in a separate thread. (I know - it may be better to run them as Task.WhenAll and let the TPL figure it out - but I want to control how many runs simultaneously)
Say I want to run 5 of them simultaneously (configurable)
I create 5 tasks (TPL), one for each job and run them in parallel.
What I want to do is to pick up the next Job in the blocking collection as soon as one of the jobs from step 4 is complete and keep going until all 50 are done.
I am thinking of creating a Static blockingCollection and a TaskCompletionSource which will be invoked when a job is complete and then it can call the consumer again to pick one job at a time from the queue. I would also like to call async/await on each job - but that's on top of this - not sure if that has an impact on the approach.
Is this the right way to accomplish what I'm trying to do?
Similar to this link, but catch is that I want to process the next Job as soon as one of the first N items are done. Not after all N are done.
Update :
Ok, I have this code snippet doing exactly what I want, if someone wants to use it later. As you can see below, 5 threads are created and each thread starts the next job when it is done with current. Only 5 threads are active at any given time. I understand this may not work 100% like this always, and will have performance issues of context switching if used with one cpu/core.
Job 2 started on thread :13. wait time:3600000ms. Time:8/29/2014 3:14:43 PM
Job 4 started on thread :14. wait time:15000ms. Time:8/29/2014 3:14:43 PM
Job 0 started on thread :7. wait time:600000ms. Time:8/29/2014 3:14:43 PM
Job 1 started on thread :12. wait time:900000ms. Time:8/29/2014 3:14:43 PM
Job 3 started on thread :11. wait time:120000ms. Time:8/29/2014 3:14:43 PM
job 4 finished on thread :14. 8/29/2014 3:14:58 PM
Job 5 started on thread :14. wait time:1800000ms. Time:8/29/2014 3:14:58 PM
job 3 finished on thread :11. 8/29/2014 3:16:43 PM
Job 6 started on thread :11. wait time:1200000ms. Time:8/29/2014 3:16:43 PM
job 0 finished on thread :7. 8/29/2014 3:24:43 PM
Job 7 started on thread :7. wait time:30000ms. Time:8/29/2014 3:24:43 PM
job 7 finished on thread :7. 8/29/2014 3:25:13 PM
Job 8 started on thread :7. wait time:100000ms. Time:8/29/2014 3:25:13 PM
job 8 finished on thread :7. 8/29/2014 3:26:53 PM
Job 9 started on thread :7. wait time:900000ms. Time:8/29/2014 3:26:53 PM
job 1 finished on thread :12. 8/29/2014 3:29:43 PM
Job 10 started on thread :12. wait time:300000ms. Time:8/29/2014 3:29:43 PM
job 10 finished on thread :12. 8/29/2014 3:34:43 PM
Job 11 started on thread :12. wait time:600000ms. Time:8/29/2014 3:34:43 PM
job 6 finished on thread :11. 8/29/2014 3:36:43 PM
Job 12 started on thread :11. wait time:300000ms. Time:8/29/2014 3:36:43 PM
job 12 finished on thread :11. 8/29/2014 3:41:43 PM
Job 13 started on thread :11. wait time:100000ms. Time:8/29/2014 3:41:43 PM
job 9 finished on thread :7. 8/29/2014 3:41:53 PM
Job 14 started on thread :7. wait time:300000ms. Time:8/29/2014 3:41:53 PM
job 13 finished on thread :11. 8/29/2014 3:43:23 PM
job 11 finished on thread :12. 8/29/2014 3:44:43 PM
job 5 finished on thread :14. 8/29/2014 3:44:58 PM
job 14 finished on thread :7. 8/29/2014 3:46:53 PM
job 2 finished on thread :13. 8/29/2014 4:14:43 PM