我有一个用作队列的 SQL Server 表,它正在由多线程(很快将成为多服务器)应用程序处理。我想要一种让进程从队列中声明下一行的方法,将其标记为“进程中”,而不会有多个线程(或多个服务器)同时声明同一行的可能性。
有没有办法更新一行中的标志并同时检索该行?我想要这样的伪代码,但理想情况下,不要阻塞整个表:
Block the table to prevent others from reading
Grab the next ID in the queue
Update the row of that item with a "claimed" flag (or whatever)
Release the lock and let other threads repeat the process
Run Code Online (Sandbox Code Playgroud)
使用 T-SQL 完成此任务的最佳方法是什么?我记得有一次看到一条语句,它会删除行,同时将删除的行存入临时表,这样你就可以对它们做其他事情,但我现在一辈子都找不到它。
在从这个问题指向它之后,我正在寻找 multiprocessing.Queue 的一个工作示例:Python using multiple processor
我遇到了这个问题,但即使我更改,它似乎也只使用我的 12 个处理器之一num_processes=12。我也改变了,num_jobs = 200000所以它不会那么快完成。
有人可以告诉我那个例子有什么问题,或者给我指出一个有效的例子吗?
入队到 ConcurrentQueue 的对象是复制到队列中还是只是它们的引用?
我不明白任何场景。
解释:
我像这样定义了一个 ConcurrentQueue:
// BufferElement is a class I created
private ConcurrentQueue<BufferElement> _bufferQueue;
Run Code Online (Sandbox Code Playgroud)
我有一个被多次调用的函数,它的目的是将一个元素加入队列:
private void EnqueueElementToBuffer(string data, int moreData)
{
// the bufferElement constructor is setting data and moreData to it's fields.
BufferElement bufferElement = new BufferElement(data, moreData);
bufferQueue.Enqueue(bufferElement);
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我会在一段时间后出现内存不足异常。我想可能是因为垃圾收集器没有收集 ,bufferElement因为它仍然在 中被引用bufferQueue,所以我将函数更改为:
private void EnqueueElementToBuffer(string data, int moreData)
{
// _bufferElement is now a filed of the class
_bufferElement.Data = data;
_bufferElement.MoreData = moreData;
bufferQueue.Enqueue(_bufferElement);
}
Run Code Online (Sandbox Code Playgroud)
而且我没有得到异常,也不会根据 Windows 任务管理器中的内存来判断。
现在我认为问题已经解决了,因为当我将对象排入队列时,只有对对象的引用被复制到队列中,但我担心队列中的所有元素都在引用同一个对象,所以我检查了另一个我在另一个线程中的功能是: …
当客户在我的网站上填写表单时,我需要调用 desk.com api 来创建案例。但是,有时 API 会因维护而停机(太频繁了!),我的调用会失败。
目前,我只是将详细信息写入登录错误并向自己发送电子邮件。然后我手动创建案例。
所以我正在考虑编写某种消息队列,而不是在进程中调用 api,我可以将请求放入队列中,然后让一些进程处理队列并进行 api 调用。如果 api 调用失败,他们会在下一个预定的时间间隔内重试。
由于世界上有如此多的 Web API,我想其他人肯定也有同样的问题。那么是否有一些第三方解决方案可以有效地完成我想要做的事情?或一些开源项目或处理这个问题的东西?
干杯!
我试图不断地侦听队列,但大约一分钟后(假设我的队列为空),我因此错误而断开连接:
DEBUG:pika.adapters.blocking_connection:Outbound buffer size: 0
DEBUG:pika.adapters.blocking_connection:Outbound buffer size: 0
ERROR:pika.adapters.base_connection:Read empty data, calling disconnect
DEBUG:pika.adapters.blocking_connection:Handling disconnect
INFO:pika.adapters.blocking_connection:on_connection_closed: None, True
WARNING:pika.adapters.blocking_connection:Received Channel.Close, closing: None
DEBUG:pika.callback:Clearing out '1' from the stack
Traceback (most recent call last):
File "controller.py", line 59, in <module>
c.run()
File "controller.py", line 55, in run
self.listen_queue() # Blocking function
File "controller.py", line 25, in listen_queue
self.channel.start_consuming() # Start consuming
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 814, in start_consuming
self.connection.process_data_events()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 168, in process_data_events
if self._handle_read():
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 272, …Run Code Online (Sandbox Code Playgroud) 我的代码:
while( Memo.qRcv.size() > 0) {
MessageReceived msg=Memo.qRcv.poll();
...
}
Run Code Online (Sandbox Code Playgroud)
然后我得到了
2014-03-01 11:09:36 DEBUG [Thread-16] (threadQueueSendtoUser.java:163) - Memo.qRcv = null, size = 41590
Run Code Online (Sandbox Code Playgroud) 我需要使用一个只包含一个元素的队列,任何新元素都会丢弃现有元素。有内置解决方案吗?
我编码的解决方案有效,但我努力不重新发明轮子:)
import Queue
def myput(q, what):
# empty the queue
while not q.empty():
q.get()
q.put(what)
q = Queue.Queue()
print("queue size: {}".format(q.qsize()))
myput(q, "hello")
myput(q, "hello")
myput(q, "hello")
print("queue size: {}".format(q.qsize()))
Run Code Online (Sandbox Code Playgroud)
编辑:遵循一些评论和答案——我知道一个变量就是为此:) 不过,在我的程序中,队列将用于进程之间的通信。
我知道PriorityQueue在add()使用对象的compareTo()方法对元素进行排序时会自动对元素进行排序。假设我想在某个时候更改队列中元素的优先级。PriorityQueue考虑到元素的优先级发生了变化,我如何明确地命令再次对自身进行排序?
我在python 2.7(在linux下)编写了一个类,它使用多个进程异步操作数据库。我遇到当使用一个很奇怪的阻塞行为multiprocessing.Queue.put()和multiprocessing.Queue.get()我无法解释。
这是我所做的简化版本:
from multiprocessing import Process, Queue
class MyDB(object):
def __init__(self):
self.inqueue = Queue()
p1 = Process(target = self._worker_process, kwargs={"inqueue": self.inqueue})
p1.daemon = True
started = False
while not started:
try:
p1.start()
started = True
except:
time.sleep(1)
#Sometimes I start a same second process but it makes no difference to my problem
p2 = Process(target = self._worker_process, kwargs={"inqueue": self.inqueue})
#blahblah... (same as above)
@staticmethod
def _worker_process(inqueue):
while True:
#--------------this blocks depite data having arrived------------
op …Run Code Online (Sandbox Code Playgroud) 我在 Laravel Forge 服务器上设置了自动 iOS 推送通知。但是,有时并非所有推送通知都会发送出去。所以,我试图通过将它们添加到队列来解决这个问题。这是我现在的代码:
$devices = DB::table('devices')->get();
foreach ($devices as $device)
{
$push = PushNotification::app('Development')
->to($device->device)
->send($message);
$push = PushNotification::app('Distribution')
->to($device->device)
->send($message);
}
Run Code Online (Sandbox Code Playgroud)
现在,我尝试这样做:
$this->dispatch($push = PushNotification::app('Distribution')
->to($device->device)
->send($message));
Run Code Online (Sandbox Code Playgroud)
但这 - 显然 - 行不通。我该如何正确执行此操作?我不得不承认,我不太了解添加到队列的语法,而且文档在这方面不是很清楚也无济于事。