最近,我面临着在我们的软件中进行多处理的挑战。我想要一个主进程来生成子进程,并且我需要某种方法将日志信息发送回主进程。这主要是因为我们使用的模块将警告和错误消息写入日志对象,并且我们希望这些消息出现在主进程中运行的 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) 假设我有一个整数队列,
#include <iostream>
#include <queue>
using namespace std;
int main() {
int firstValToBePushed = 1;
queue<int> CheckoutLine;
CheckoutLine.push(firstValeToBePushed);
cout << CheckoutLine.front();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能使用保存指向整数的指针而不是像上面当前那样的整数来完成基本上相同的事情。我计划创建一个循环来生成多个值,但这只是一个更简单的示例。
谢谢,
这是我在这里发表的第一篇文章,因此如果我不遵守格式规则或其他一般规则,我提前道歉。
我有一个基于 Spring JMS 的客户端应用程序,它使用多个使用者从队列中读取数据。我的限制是:在失败和消息重复检测的情况下保证重新传递。
弹簧配置
<bean id="jndiDestinationResolver" class="org.springframework.jms.support.destination.DynamicDestinationResolver" />
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="MyConnectionFactory" />
<property name="destinationResolver" ref="jndiDestinationResolver" />
<property name="receiveTimeout" value="100" />
</bean>
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="destinationResolver" ref="jndiDestinationResolver" />
<property name="connectionFactory" ref="MyConnectionFactory" />
<property name="destinationName" value="my_Queue" />
<property name="messageListener" ref="MyListener" />
<property name="maxConcurrentConsumers" value="10"/>
<property name="sessionTransacted" value="true"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
我的监听器代码类似于:
protected void processTextMessage(TextMessage textMessage) {
尝试 {
// 处理消息
} catch(异常e){
尝试 {
sendTextMessage("my_Queue",correlationID, textMessage.getText());
} catch (JMSException e1) {
log.error("将消息写回队列时出错!");
抛出 JmsUtils.convertJmsAccessException(e1);
}
}
}
protected void sendTextMessage(字符串队列名称,最终字节[]相关ID,字符串消息){ … 这个想法相当简单,对于任务来说,它需要是一个队列,所以请不要建议其他方法。
我需要一个完整的队列,例如 (5,4,3,2,1) 的 5 个元素,并且用户必须输入他们想要移动到前面的元素的位置。例如,位置 3,其元素为 2。因此新队列将是:2,5,4,3,1...
我已经在这方面工作了很长时间,所以我不会一碰壁就寻求帮助。只是想让我再次朝着正确的方向努力:) 谢谢
我的环境:
我正在尝试设置 POSIX 消息队列的属性,但代码不会更改该属性。我只获得默认属性值。
你能指出我的代码有什么问题吗?
我以用户(而不是 root)身份执行 a.out。
#include <stdio.h>
#include <mqueue.h> // for message queue
#include <sys/stat.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <string.h>
/*
gcc [file] -lrt
*/
static void showAttr(mqd_t mqd)
{
struct mq_attr attr;
mq_getattr(mqd, &attr);
printf("maxmsg = %d\n", attr.mq_maxmsg);
printf("msgsize = %d\n", attr.mq_msgsize);
printf("curmsgs = %d\n", attr.mq_curmsgs);
}
int main()
{
mqd_t mqd;
int flags;
int ret;
struct mq_attr attr;
flags = O_RDWR | O_CREAT;
attr.mq_flags = 0; // or …Run Code Online (Sandbox Code Playgroud) 我使用 Redis 服务器来排队工作作业和缓存,并且由于我不希望键冲突,所以我想知道 Python-RQ 使用哪些键名称来存储其数据。
我正在尝试使用节点应用程序发送到 SQS 的消息。因此,“推送”操作由服务器 A 上的 Node App 执行,“监听”操作由服务器 B 上的 Laravel App 执行。
我的问题:我不知道如何格式化要使用的有效负载php artisan queue:work。
以前有人经历过这个并找到解决方案吗?
谢谢!
语境
我一直在查看 多处理队列的源代码(请参见此处)Python 2.7并有一些问题。
双端队列用于缓冲区,放入队列的任何项目都会附加到双端队列,但对于 get(),使用管道。
我们可以看到,在 put 期间,如果 feeder 线程尚未启动,它将启动。
该线程将从线程中弹出对象并将它们发送到上述管道的读取端。
问题
那么,为什么要使用双端队列和管道呢?
难道不能只使用双端队列(或任何其他具有 FIFO 行为的数据结构)并同步推送和弹出吗?
同样,难道不能只使用 Pipe 来包装发送和接收吗?
也许我在这里遗漏了一些东西,但是馈线线程弹出物品并将它们放在管道上似乎有点矫枉过正。