标签: queue

TPL队列处理

我目前正在开发一个项目,我需要排队一些工作进行处理,这是要求:

  1. 必须一次处理一个作业
  2. 必须能够等待排队的项目

所以我想要类似于:

Task<result> QueueJob(params here)
{
   /// Queue the job and somehow return a waitable task that will wait until the queued job has been executed and return the result.
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过后台运行任务,只是将项目从队列中拉出来并处理作业,但难度是从后台任务到方法.

如果需要的话,我可以在QueueJob方法中使用刚刚请求完成回调的路径,但是如果我能得到一个透明的任务可以让你等待处理工作(即使有工作)也会很棒在它排队之前).

c# queue task-parallel-library

2
推荐指数
1
解决办法
5578
查看次数

如何在Python中"监听"多处理队列

我将从代码开始,我希望它很简单:

import Queue
import multiprocessing


class RobotProxy(multiprocessing.Process):

    def __init__(self, commands_q):
        multiprocessing.Process.__init__(self)

        self.commands_q = commands_q


    def run(self):
        self.listen()
        print "robot started"


    def listen(self):

        print "listening"
        while True:
            print "size", self.commands_q.qsize()
            command = self.commands_q.get()
            print command
            if command is "start_experiment":
                self.start_experiment()
            elif command is "end_experiment":
                self.terminate_experiment()
                break
            else: raise Exception("Communication command not recognized")
        print "listen finished"


    def start_experiment(self):
        #self.vision = ds.DropletSegmentation( )
        print "start experiment"


    def terminate_experiment(self):
        print "terminate experiment"



if __name__ == "__main__":

    command_q = Queue.Queue()
    robot_proxy = RobotProxy( command_q ) …
Run Code Online (Sandbox Code Playgroud)

python queue multiprocessing python-2.7

2
推荐指数
1
解决办法
1654
查看次数

这是比较两个GCD队列的正确方法吗?

在关于SO的早期问题之后,我现在想要比较两个不同的大型中央调度队列,以尝试确定当前代码是否在主线程上运行.我的问题很简单:这是实现这一目标的有效方法吗?或者是否有一些我没有考虑过这样做的陷阱?

if (dispatch_get_current_queue() != dispatch_get_main_queue()) {

    // We are currently on a background queue

} else {

    // We are on the main queue
}
Run Code Online (Sandbox Code Playgroud)

干杯

queue grand-central-dispatch ios

2
推荐指数
1
解决办法
890
查看次数

在Python中实现队列

我试图在Python中实现一个队列.但是每次我运行我的代码时,我都会收到消息"AttributeError:Queue实例没有属性'队列'"我已经挣扎了一个多小时左右.非常感谢任何帮助.

我的代码:

class Queue:

    def __int__(self):
        '''initilize a empty queue'''
        self.queue = []

    def dequeue(self):
        '''remove and return the last element'''
        return self.queue.pop()

    def enqueue(self, val):
        '''Add element to the end'''
        self.queue.append(val)   

    def is_empty(self):
        '''Return True if empty queue'''
        return len(self.queue) == 0

if __name__== '__main__':

    q = Queue()
    for i in range(0,11):
        q.enqueue(i)
    while not q.is_empty():
        print q.dequeue()  
Run Code Online (Sandbox Code Playgroud)

python queue

2
推荐指数
1
解决办法
1015
查看次数

C++中的数据结构是否可以在达到指定大小阈值时自动解除?

我正在寻找一个队列的现有实现,它将构建到指定的大小,然后如果将更多元素排入队列,则在插入新元素之前将最后一个元素解除,从而保持其大小始终<=指定大小.

我知道这很容易实现std::list,但我很想知道在C++中是否已存在这样的数据结构?

c++ queue data-structures

2
推荐指数
1
解决办法
77
查看次数

为什么我的Perl线程陷入无限循环?(线程::队列)

我正在使用Thread::Queue将数组推送到队列并使用线程处理它的每个元素.下面是我的程序的简化版本,用于演示正在发生的事情.

#!/usr/bin/perl

use strict;
use warnings;

use threads;
use threads::shared;
use Thread::Queue;

# Define queue
my $QUEUE :shared = new Thread::Queue();

# Define values
my @values = qw(string1 string2 string3);

# Enqueue values
$QUEUE->enqueue(@values);

# Get thread limit
my $QUEUE_SIZE   = $QUEUE->pending();
my $thread_limit = $QUEUE_SIZE;

# Create threads
for my $i (1 .. $thread_limit) {
    my $thread = threads->create(\&work);
}

# Join threads
my $i = 0;
for my $thread (threads->list()) {
    $thread->join();
}

print "COMPLETE\n";

# …
Run Code Online (Sandbox Code Playgroud)

queue perl multithreading infinite-loop

2
推荐指数
1
解决办法
393
查看次数

Amazon批处理作业进程队列

我正在使用AWS EC2实例进行生物信息学工作.我有一个(~1000)个大文件,应该使用EC2实例上的脚本进行处理,结果应该上传回S3存储桶.我想将作业(文件)分发给多个EC2实例,优先以现货价格开始.

我需要的是一个简单易用的排队系统(可能是AWS SQS或其他东西),它可以将作业分配到实例并在实例失败时重新启动作业(由于现货价格过高或其他原因).我研究过AWS SQS示例,但这些示例过于先进,通常涉及自动扩展和复杂的消息生成应用程序.

有人能从概念上指出如何以最简单的方式解决这个问题吗?AWS SQS这个简单应用的任何示例?应该如何启动一堆实例以及如何告诉他们收听队列?

对于每个输入文件,我的工作流程基本上都是这样

aws s3 cp s3://mybucket/file localFile ## Possibly streaming the file without copy
work.py --input localFile --output outputFile
aws s3 cp outputFile s3://mybucket/output/outputFile
Run Code Online (Sandbox Code Playgroud)

queue message-queue amazon-ec2 amazon-web-services

2
推荐指数
2
解决办法
3840
查看次数

从php调用Artisan Queue Retry

我有关于从php调用artisan队列的问题.我想在Beanstalkd队列中创建失败作业列表并创建一个重试按钮.

我想打电话 php artisan queue:retry{id}

所以,我把这段代码放在route.php中

Route::get('retry/{id}', function($id){
    Artisan::call("queue:retry $id");
});
Run Code Online (Sandbox Code Playgroud)

它返回错误

带有消息'Command'队列的InvalidArgumentException':未定义重试12".

你的意思是其中之一吗?queue:retry queue:forget queue:failed-table queue:flush queue:failed queue:listen queue:subscribe queue:work'

任何人都可以帮助我吗?谢谢

php queue beanstalkd laravel

2
推荐指数
2
解决办法
1458
查看次数

Python - 多处理的奇怪行为 - join不会执行

我正在使用multiprocessingpython模块.我有大约20-25个任务同时运行.每个任务将创建一个pandas.DataFrame~20k行的对象.问题是,所有任务都执行得很好,但是当谈到"加入"流程时,它就会停止.我尝试过"小"DataFrames并且效果很好.为了说明我的观点,我在下面创建了代码.

import pandas
import multiprocessing as mp

def task(arg, queue):
    DF = pandas.DataFrame({"hello":range(10)}) # try range(1000) or range(10000)
    queue.put(DF)
    print("DF %d stored" %arg)

listArgs = range(20)
queue = mp.Queue()
processes = [mp.Process(target=task,args=(arg,queue)) for arg in listArgs]

for p in processes:
    p.start()

for i,p in enumerate(processes):
    print("joining %d" %i)
    p.join()

results = [queue.get() for p in processes]
Run Code Online (Sandbox Code Playgroud)

编辑:

使用DF = pandas.DataFrame({"hello":range(10)})我有一切正确:"DF 0存储"直到"DF 19存储",与"加入0"到"加入19"相同.

然而,DF = pandas.DataFrame({"hello":range(1000)})问题出现了:当它存储DF时,加入步骤在"加入3"之后停止.

感谢有用的提示:)

python queue multiprocessing dataframe python-multiprocessing

2
推荐指数
1
解决办法
1931
查看次数

从boost lock free spsc队列获取错误输出

我正在尝试使用boost库实现用户定义数据类型的无锁队列,但是我得到了错误的结果.

请帮我解决我做错的地方.

#include <boost/lockfree/spsc_queue.hpp>
#include <thread>
#include <iostream>
#include <string.h>
#include <time.h>   


class Queue
{
 private:
       unsigned char *m_data;
       int m_len;

 public:
       Queue(unsigned char *data,int len);

       Queue(const Queue &obj);

       ~Queue();  

       Queue & operator =(const Queue &obj);


       unsigned char *getdata()
       {
         return m_data;
       }

       int getint()
       {
           return m_len;
       }
};

Queue::Queue(unsigned char* data, int len)
{
      m_len=len;

      m_data=new unsigned char[m_len];

      memcpy(m_data,data,m_len);
}

Queue::Queue(const Queue& obj)
{
      m_len=  obj.m_len;

      m_data=new unsigned char[m_len];

      memcpy(m_data,(unsigned char *)obj.m_data,m_len);
}

Queue::~Queue()
{
   delete[] m_data;
   m_len=0; …
Run Code Online (Sandbox Code Playgroud)

queue boost lock-free c++11

2
推荐指数
1
解决办法
1059
查看次数