我正在连接到 IBM Websphere MQ。我希望能够将回复消息与正确的请求消息相匹配。我已经翻阅了数百页才能得到这个,但没有运气。
我有一个类 - MQHandler - 它将一条消息发送到一个定义的队列,并从另一个队列读取请求。这工作得很好,但是,如果多个用户同时使用该应用程序,消息就会混淆。
我似乎无法在接收器上找到一种方法来指示要匹配的 CorrelationID。就像是...
消费者.接收(选择器);
您可以检查以下方法以确保我正确执行此操作吗?
/**
* When the class is called, this initialisation is done first.
*
* @throws JMSException
*/
public void init() throws JMSException
{
// Create a connection factory
JmsFactoryFactory ff;
try
{
ff = JmsFactoryFactory.getInstance( WMQConstants.WMQ_PROVIDER );
cf = ff.createConnectionFactory();
// Set the properties
cf.setStringProperty( WMQConstants.WMQ_HOST_NAME, hostServer );
cf.setIntProperty( WMQConstants.WMQ_PORT, 1414 );
cf.setStringProperty( WMQConstants.WMQ_CHANNEL, channel );
cf.setIntProperty( WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT );
cf.setStringProperty( WMQConstants.WMQ_QUEUE_MANAGER, qManager );
connection = cf.createConnection(); …Run Code Online (Sandbox Code Playgroud) 最近,我面临着在我们的软件中进行多处理的挑战。我想要一个主进程来生成子进程,并且我需要某种方法将日志信息发送回主进程。这主要是因为我们使用的模块将警告和错误消息写入日志对象,并且我们希望这些消息出现在主进程中运行的 GUI 中。
显而易见的方法是编写一个带有 write() 方法的小类,该方法将 put() 放入队列,然后在日志流处理程序中使用此类。然后主进程将从该队列中 get() 将文本发送到 gui。但这似乎不起作用,我不知道为什么
我编写了一些示例代码来演示该问题。它使用日志对象在子进程中写入队列,然后主进程尝试从队列中读取,但失败。有人可以帮我弄清楚这有什么问题吗?
import time, multiprocessing, queue, logging
class FileLikeQueue:
"""A file-like object that writes to a queue"""
def __init__(self, q):
self.q = q
def write(self, t):
self.q.put(t)
def flush(self):
pass
def func(q):
"""This function just writes the time every second for five
seconds and then returns. The time is sent to the queue and
to a logging object"""
stream = FileLikeQueue(q)
log = logging.getLogger()
infohandler = logging.StreamHandler(stream)
infohandler.setLevel(logging.INFO)
infoformatter = …Run Code Online (Sandbox Code Playgroud) 在一次面试中,我被要求列出目录及其子目录\xc2\xb9中的文件名,而不使用递归,也不使用堆栈或队列。
\n\n由于我知道的唯一非递归方式使用堆栈,因此我无法回答这个问题。
\n\n面试官解释了解决方案,但我无法理解。我唯一记得的是它涉及两种方法而不是一种。
\n\n这种允许在没有递归、没有堆栈或队列的情况下列出目录及其子目录中的文件的方法是什么?
\n\n\xc2\xb9 解决方案与语言无关。子目录列表由ListDirectories(string directoryPath)方法提供,文件由ListFiles(string directoryPath). 我们事先不知道子目录的深度。
我在大学的实时系统课程中使用 ARM 微控制器。在我目前正在进行的项目中,我正在实现矢量场直方图(VFH)算法。
问题是:我需要在线程之间进行通信;更具体地说,我想要一个线程从测距仪获取传感器数据,对其进行必要的转换并将它们存储在队列中。他们,另一个线程必须获取这些数据并处理它,等等。
目前,我正在使用它的一个更简单的版本 - 一个线程从 ADC (SensorAcquisitionHandler) 获取数据,另一个线程将前 5 项(最多)的平均值输出到显示器 (ControlSignalHandler)。
/* Queue used to store data from the rangefinder sensors. */
static unsigned int Sensors[100];
static int SensorsHead = 0;
static int SensorsTail = 0;
void SensorAcquisitionHandler(void) {
/* Clear the interrupt. */
ADCIntClear(ADC0_BASE, 1);
int i; /* Index for the measurements buffer. */
/* There are only 3 rangefinders used. */
if (ADCSequenceDataGet(ADC0_BASE, 1, rangeBuffer) == 3) {
/* Put rangeBuffer's data into SensorDataQueue. */
/* …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)
但这 - 显然 - 行不通。我该如何正确执行此操作?我不得不承认,我不太了解添加到队列的语法,而且文档在这方面不是很清楚也无济于事。
我有一个生成器会生成超过 1 万亿个字符串,我想把它们放在一个队列中,让一个工作池来消耗这个队列。但是,我无法将整个 1 万亿个字符串放入内存中并将它们映射到线程。
生成器非常快,消费工人不是。我需要将队列的长度保持在一定水平,以免破坏我的记忆。这意味着我需要找到一种方法来暂停和重新启动队列。
任何人都可以提供有关如何在 Python 3.4 中完成此任务的提示吗?
我正在使用 mailgun API 发送电子邮件。我错误地调用了一个在 Mailgun 中排队数千封电子邮件的函数。
有没有办法取消/清除队列?