在这种情况
下,真的ArrayDeque应该优先选择吗?为什么 ArrayDeque 比 LinkedList 更好?LinkedList
在我看来,我应该使用 LinkedList 而不是 ArrayDeque,因为这个算法中有相当多的pollandoffer
操作,并且没有对元素的随机访问。
public ArrayList<ArrayList<Integer>> levelOrder(TreeNode a) {
Queue<TreeNode> q = new LinkedList<>(); // new ArrayDeque<>() ???
q.offer(a);
ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
while (q.peek() != null){ //returns null if empty
ArrayList<Integer> list = new ArrayList<>();
int n = q.size();
for (int i = 0; i < n; i++){
TreeNode node = q.poll();
list.add(node.val);
if (node.left != null) {
q.offer(node.left);
}
if (node.right != …Run Code Online (Sandbox Code Playgroud) 演示代码
在文件A中:
class Queuetest{
...
queue<myclass>* mMyQueue = new queue<myclass>;
void addMyclass(myclass& myclassobject){
mMyQueue->push(myclassobject);
}
...
};
Run Code Online (Sandbox Code Playgroud)
在文件B中:
...
Queuetest* mQueuetest = new Queuetest();
mQueuetest->addMyclass(new myclass(...));
...
Run Code Online (Sandbox Code Playgroud)
所以编译告诉我:
对“myclass”类型的引用无法绑定到“myclass *”类型的右值
一些类似的案例给了我原因:
C++ 标准不允许将匿名临时值绑定到引用,尽管某些编译器允许将其作为扩展。(允许绑定到 const 引用。)
但没有办法告诉我如何解决;
我想使用队列来控制我的对象,并使该对象像先进先出(FIFO)一样工作;添加新对象,并弹出最后一个对象,自动释放该对象的内存;
有没有好的方法可以做到这一点?
在 Python 3.6 中,我并行运行多个进程,其中每个进程 ping 一个 URL 并返回一个 Pandas 数据帧。我想继续运行(2+)个进程,我创建了一个最小的代表性示例,如下所示。
我的问题是:
1)我的理解是,由于我有不同的功能,我不能使用Pool.map_async()及其变体。是对的吗?我见过的唯一例子是重复相同的功能,就像这个答案一样。
2)使此设置永久运行的最佳实践是什么?在下面的代码中,我使用了一个while循环,我怀疑它不适合此目的。
3)Process我使用和 的方式是Manager最佳的吗?我使用multiprocessing.Manager.dict()共享字典来返回进程的结果。我在这个答案的评论中看到,使用Queuehere是有意义的,但是该Queue对象没有“.dict()”方法。所以,我不确定这会如何运作。
对于示例代码的任何改进和建议,我将不胜感激。
import numpy as np
import pandas as pd
import multiprocessing
import time
def worker1(name, t , seed, return_dict):
'''worker function'''
print(str(name) + 'is here.')
time.sleep(t)
np.random.seed(seed)
df= pd.DataFrame(np.random.randint(0,1000,8).reshape(2,4), columns=list('ABCD'))
return_dict[name] = [df.columns.tolist()] + df.values.tolist()
def worker2(name, t, seed, return_dict):
'''worker function'''
print(str(name) + …Run Code Online (Sandbox Code Playgroud) 问题
我正在分派一个作业来执行一个需要准备好资源才能正确执行的操作,因此如果失败,则需要在一段时间后重试。但真正发生的情况是,如果失败,就不会再次执行。我正在使用 Supervisor 来管理队列和数据库驱动程序,并且我在默认的queue.php配置文件中没有进行任何更改。
使用 Laravel 5.8。
我尝试过的
我已经尝试过手动设置作业类中的尝试次数,例如
public $tries = 5;
Run Code Online (Sandbox Code Playgroud)
以及与重试延迟相同的事情
public $retryAfter = 60;
Run Code Online (Sandbox Code Playgroud)
我的代码
我基于使用 make:job 制作的默认作业模板来实现此作业,我的构造函数和处理方法如下所示:
public function __construct($event, $data)
{
$this->event = $event;
$this->data = $data;
}
public function handle()
{
Log::info('Job started | ' . $this->event . ' | Attempt: ' . $this->attempts());
// Executes some logic and throws an Exception if it fails
Log::info('Job succeeded | ' . $this->event);
}
Run Code Online (Sandbox Code Playgroud)
最后,它不会到达“作业成功”日志,并且不会记录除 1 之外的任何其他尝试。
我是否缺少一些概念或者这段代码是否有问题?
给出以下最小示例:
# myproject.py
import time
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
time.sleep(5)
return 'hello'
if __name__ == '__main__':
app.run(host='0.0.0.0')
Run Code Online (Sandbox Code Playgroud)
# wsgi.py
from myproject import app
if __name__ == '__main__':
app.run()
Run Code Online (Sandbox Code Playgroud)
uwsgi --http-socket 0.0.0.0:8080 --workers 1 --listen 2 --module wsgi:app
Run Code Online (Sandbox Code Playgroud)
我现在预计,同时发送 3 个以上的请求(1 个在工作线程中正在进行,2 个已排队)将导致只有 3 个请求得到服务,而其他请求则被拒绝。
然而,情况似乎并非如此。当像这样发送 10 个请求时
curl http://127.0.0.1:8080 & \
curl http://127.0.0.1:8080 & \
curl http://127.0.0.1:8080 & \
curl http://127.0.0.1:8080 & \
curl http://127.0.0.1:8080 & \
curl http://127.0.0.1:8080 & \
curl http://127.0.0.1:8080 & …Run Code Online (Sandbox Code Playgroud) 我有一个用例,其中有一个带有 lambda 函数的 Amazon SQS fifo 队列。我需要确保 fifo 仅在前一个 lambda 执行完成时才触发 lambda(事件也按顺序出现)。从 aws 文档来看,fifo 支持一次处理,但它没有在任何地方提到它不会在 lambda 上推送更多事件,直到第一条消息完全处理为止。
我需要确保仅当 lambda 函数完全处理前一条消息时才处理下一条消息。
有没有办法确保当消息 1 完全由 lambda 处理时,消息 2 只由 lambda 处理?
queue message-queue amazon-sqs amazon-web-services aws-lambda
我正在尝试通过缓存加载 Shopify 产品,并使用 IMRedis::throttle来防止 Shopify 中的速率限制。我的问题是我把所有的获取过程都放在 a 中Batch,它返回一个负数。待处理的作业。
这是我的方法
公共函数加载(请求$请求,样本$样本){
return DB::transaction(function () use ($sample) {
$batch = Bus::batch([])->dispatch();
$since_id = 0;
while ($since_id >= 0) {
$fetchedProducts = (new ShopifyProduct(
$sample->shopify_domain,
$sample->shopify_info['access_token'])
)
->getById($since_id)
->then(function ($data) {
return $data['products'];
}, function () {
return [];
})
->wait();
if (collect($fetchedProducts)->count() == 0) break;
$lastProduct = Arr::last($fetchedProducts);
$since_id = $lastProduct['id'];
collect($fetchedProducts)
->each(function ($shopifyProduct) use($merchant, &$batch) {
$batch->add(new CacheProducts($shopifyProduct, $sample));
});
}
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用c ++中的pthreads编写一个线程安全的队列.我的程序有93%的时间都在工作.另外7%的时间它会吐出垃圾,或者似乎睡着了.我想知道我的队列中是否存在一些上下文切换会破坏它的缺陷?
// thread-safe queue
// inspired by http://msmvps.com/blogs/vandooren/archive/2007/01/05/creating-a-thread-safe-producer-consumer-queue-in-c-without-using-locks.aspx
// only works with one producer and one consumer
#include <pthread.h>
#include <exception>
template<class T>
class tsqueue
{
private:
volatile int m_ReadIndex, m_WriteIndex;
volatile T *m_Data;
volatile bool m_Done;
const int m_Size;
pthread_mutex_t m_ReadMutex, m_WriteMutex;
pthread_cond_t m_ReadCond, m_WriteCond;
public:
tsqueue(const int &size);
~tsqueue();
void push(const T &elem);
T pop();
void terminate();
bool isDone() const;
};
template <class T>
tsqueue<T>::tsqueue(const int &size) : m_ReadIndex(0), m_WriteIndex(0), m_Size(size), m_Done(false) {
m_Data = new T[size]; …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
Iterator it = queue.iterator();
while(it.hasNext()){
random = randNumber(1,2);
if(random == 1){
queue.poll();
} else {
queue.add("new");
queue.poll();
}
}
Run Code Online (Sandbox Code Playgroud)
它给了我:
Exception in thread "test" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:761)
at java.util.LinkedList$ListItr.next(LinkedList.java:696)
Run Code Online (Sandbox Code Playgroud)
编辑@Jon Skeet:
我想做的是:
我正在使用队列api并遇到了导致程序崩溃的错误.
首先,我从字典中获取队列,并在打印输出中返回该队列
获取的队列是[{[],[]}]
这是正常的吗?队列是否正确创建?
然后,当我尝试添加到队列或获取其长度时,我得到两个错误badargs.
TorrentDownloadQueue = dict:fetch(torrentDownloadQueue, State),
io:format("The fetched queue is ~p~n", [dict:fetch(torrentDownloadQueue, State)]),
% Add the item to the front of the queue (main torrent upload queue)
TorrentDownloadQueue2 = queue:in_r(Time, TorrentDownloadQueue),
% Get the lenght of the downloadQueue
TorrentDownloadQueueLength = queue:len(TorrentDownloadQueue2),
Run Code Online (Sandbox Code Playgroud)
当我尝试插入值10时,错误是
**终止原因==**{badarg,[{queue,in_r,[10,[{[],[]}]]},{ph_speed_calculator,handle_cast,2},{gen_server,handle_msg,5},{ proc_lib,init_p_do_apply,3}]}**异常退出:函数队列中的badarg:in_r/2称为队列:in_r(10,[{[],[]}])来自ph_speed_calculator的调用:handle_cast/2 in call from gen_server:从proc_lib调用handle_msg/5:init_p_do_apply/3 13>
这是in_r的错误,但我也得到了len调用的badargs错误.
我称之为或者初始队列不正确的方式有什么问题?我按如下方式创建队列并将其添加到字典中
TorrentDownloadQueue = queue:new(),
TorrentUploadQueue = queue:new(),
D4 = dict:store(torrentDownloadQueue, [TorrentDownloadQueue], D3),
D5 = dict:store(torrentUploadQueue, [TorrentUploadQueue], D4),
Run Code Online (Sandbox Code Playgroud)
我不知道我做错了什么.
queue ×10
c++ ×2
java ×2
laravel ×2
php ×2
algorithm ×1
amazon-sqs ×1
aws-lambda ×1
class ×1
concurrency ×1
constants ×1
erlang ×1
exception ×1
jobs ×1
pthreads ×1
python ×1
python-3.x ×1
reference ×1
request ×1
shopify ×1
uwsgi ×1
webserver ×1
while-loop ×1