我在全局globqueue数组中存储了一堆队列。我现在想遍历所有这些队列并获取其中的所有项目。
不过,我在从队列中获取项目时遇到了一些问题。我正在使用 .get_nowait(),如果队列中没有任何内容,它将抛出“空”异常。我以为我可以像这样抓住它:
for index, item in enumerate(globqueue):
print index, item
iterme = 1
while iterme:
try:
getiterme = item.get_nowait()
print getiterme
except ValueError:
iterme = 0
continue
Run Code Online (Sandbox Code Playgroud)
但是我仍然收到这个错误,其余的代码不会继续:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
self.run()
File "server.py", line 213, in run
getiterme = item.get_nowait()
File "/usr/lib/python2.6/Queue.py", line 190, in get_nowait
return self.get(False)
File "/usr/lib/python2.6/Queue.py", line 165, in get
raise Empty
Empty
Run Code Online (Sandbox Code Playgroud)
我知道我可以先用 来检查队列的大小.qsize(),但我也读到这并不总是那么准确,所以......请求宽恕比许可更好,对吧?
在标准预取队列之后,我通过一些额外的验证代码扩展了前面提到的示例,请参阅附带的代码.也就是说,每个第i个训练步骤,在验证集上评估学习模型(在我的例子中是几个).验证集不能通过队列提供,因此一个可能的想法是使用共享变量构建额外的推理图.
这在某种程度上有效,但是在训练结束后,程序挂起(at coord.join())并最终抛出异常:Coordinator stopped with threads still running:...然后异步加载线程也抛出异常.该coordinator异常可以通过得到解决try/except条款(见下面的代码),但异步线程仍然抛出一个异常(这不妨碍主程序,虽然,但在我的opinon不应该发生的---它的while循环,应该告诉它停止).
有趣的是,如果在没有运行任何评估代码的情况下完成培训(即if (it+1)%stop == 0:注释掉后的块),则coord.join()根本不会挂起.
我的问题:我在这里做错了什么?似乎.request_stop()没有做我希望它应该做的事情?
import tensorflow as tf
import numpy as np
# some parameters
btsz = 100 # batch size
some_shape = 20 # size of one input (no of dims)
iters = 1000 # that many single training steps
ith = 10 # run validation sets every so often
# datastores (sort …Run Code Online (Sandbox Code Playgroud) 我有以下对象,
public class Pair {
private int row;
private int col;
public Pair(int row, int col){
this.row = row;
this.col = col;
}
public int getRow(){
return row;
}
public int getCol(){
return col;
}
}
Run Code Online (Sandbox Code Playgroud)
我将这些对存储在一个队列中,但是不想检查Queue是否已包含该对.这是我的代码.
Queue<Pair> queue = new LinkedList<>();
if(!queue.contains(new Pair(curr.getRow(), curr.getCol()){
//do something
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,Queue正在存储重复值.有人可以帮助我理解为什么以及解决它的方法是什么?
我正在创建NATS go lang Queue Subscriber客户端,如下所示:
nc.QueueSubscribe("foo", "my_queue", func(msg *nats.Msg) {
log.Printf("Message :%s", string(msg.Data))
})
Run Code Online (Sandbox Code Playgroud)
因此,每当我向“ foo”主题发布任何消息时,有时就会收到消息,而有时则不会。
例如,假设我向“ foo”主题发送了10条消息,那么它将最多收到2或3条消息。
我的要求如下
任何帮助表示赞赏。
Tensorflow中有读取文件的功能,但是这些功能接受文件名队列。
这意味着,当我从文件本身读取文件时,我有义务正确推断标签。
不幸的是,我在内存中有一个元组列表,其中每个元组都由文件名和标签组成。即标签不在文件中,而是在内存中。
是否可以以某种方式或以其他方式创建两个同步队列以从不同来源获取数据和标签?
更新
我写了这样的东西,但是失败了
数据= [[os.path.join(corpus_dir,文件名),标签](数据中的(文件名,标签)]
def read_my_file():
records = tf.train.input_producer(data)
record = records.dequeue()
filename = record[0]
filenames = tf.FIFOQueue(1, tf.string)
filenames.enqueue(filename)
label = record[1]
reader = tf.WholeFileReader()
key, raw = reader.read(filenames)
image = tf.image.decode_png(raw)
return image, label
image, label = read_my_file()
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(10):
image1, label1 = sess.run(image, label)
print(label1)
Run Code Online (Sandbox Code Playgroud)
这data是Python内存中的元组的列表,并且filenames是我组织来供稿文件读取器的队列。
看起来很糟糕,无法正常工作:
...test05.py", line 37, in <module>
image1, …Run Code Online (Sandbox Code Playgroud) 而不是Stack,Queue也可以用于它.为什么JVM对本地变量使用"Stack"并调用内存管理方法?我已经谷歌我的问题,但没有得到关于它的确切答案.
我有一个包含如下元素的队列:
1 None 2 3 None 4 5 6 7 None 8 9 10 11 12 13 14 15
Run Code Online (Sandbox Code Playgroud)
这里没有做分隔符的工作,仅此而已.
现在我想打印输出如下:
8 9 10 11 12 13 14 15
4 5 6 7
2 3
1
Run Code Online (Sandbox Code Playgroud)
我能打印出这个输出:
15 14 13 12 11 10 9 8
7 6 5 4
3 2
1
Run Code Online (Sandbox Code Playgroud)
从右侧遍历队列.
这是代码:
length = len(sequenced_que)
for i in range(0, length):
temp = sequenced_que.pop()
if temp is None:
print()
else:
print(temp.data, end=" ")
8 9 10 11 12 13 …Run Code Online (Sandbox Code Playgroud) 我尝试切换到FIFO队列并尝试从队列中删除消息时收到此错误消息
Value {VALUE} for parameter ReceiptHandle is invalid. Reason: The receipt handle has expired.
Run Code Online (Sandbox Code Playgroud)
似乎发生错误是因为我试图在visibility timeout过期后删除该消息。我将默认可见性超时0更改为最大12小时,这部分解决了问题。有时可能会发生,一条消息仍在我的队列中超过12个小时,然后我才能执行并删除该消息,因此我将再次收到错误消息。有什么解决方案可以将可见性超时增加12小时以上,或通过其他方式绕过此错误?
我很难理解为什么这个简单的程序最后会引发一个错误EOFError。
我正在使用与Queue()进行通讯,Thread()因此我想自动干净地终止atexit程序。
import threading
import multiprocessing
import atexit
class MyClass:
def __init__(self):
self.queue = None
self.thread = None
def start(self):
self.queue = multiprocessing.Queue()
self.thread = threading.Thread(target=self.queued_writer, daemon=True)
self.thread.start()
# Remove this: no error
self.queue.put("message")
def queued_writer(self):
while 1:
msg = self.queue.get()
print("Message:", msg)
if msg is None:
break
def stop(self):
self.queue.put(None)
self.thread.join()
instance = MyClass()
atexit.register(instance.stop)
# Put this before register: no error
instance.start()
Run Code Online (Sandbox Code Playgroud)
这引起了:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", …Run Code Online (Sandbox Code Playgroud) 是否有一个简单的程序,演示如何在Go中运行队列.我只需要在队列中添加1到10之类的东西,并使用另一个线程并行地从队列中拉出那些.
queue ×10
python ×5
go ×2
java ×2
stack ×2
tensorflow ×2
algorithm ×1
amazon-sqs ×1
concurrency ×1
exception ×1
jvm ×1
nats.io ×1
python-3.x ×1
subscription ×1