I am running a number of threads and collecting there result on a queue. I would like to dump it into array or list so that I can do indexing and retrieve those results. Each of the elements in the queue is a array of dimension n. I would like to access those arrays. Would you please let me know, how would i do it?
def dump_queue(model_queue):
queue_list = []
for i in iter(model_queue.get,'STOP'):
queue_list.append(i)
return queue_list
aux_model=train_svm(np.array(trainExample),np.array(trainLabel))
model_queue.put(aux_model.coef_)
Thus the arrays are the learned model parameters of svm
. model_queue is shared among the threads. I want to access each of the model parameters vectors not each of the entries of a model parameters.
Implementation using list. List is a Python's built-in data structure that can be used as a queue. Instead of enqueue() and dequeue(), append() and pop() function is used.
The pop() method is used to remove and return the right most element from the queue, and popleft() method is used to remove and return left most element from the queue.
Use the collections. deque class to convert a list to a queue in Python, e.g. deq = deque(my_list) . The deque class can be passed an iterable, such as a list, and initializes a new deque object.
You can not. Because it is not a sequence. If you want to index it, probably make a list . For a queue q , list(q) won't work as q is not iterable.
You're done with the parallel part and just want to get the results in a list, is that it? Then try:
list(my_queue.queue)
Example:
from queue import Queue
q = Queue()
for i in range(5):
q.put(i)
l = list(q.queue)
print(l)
Output:
[0, 1, 2, 3, 4]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With