我正在使用多处理的进程和队列.我并行启动了几个函数,大多数表现得很好:它们完成后,它们的输出转到它们的Queue,它们显示为.is_alive()== False.但由于某种原因,一些功能不起作用.它们总是显示.is_alive()== True,即使在函数的最后一行(一个打印语句说"已完成")完成之后.无论我发布的功能集是什么,都会发生这种情况,即使它只有一个.如果不是并行运行,则函数表现良好并正常返回.什么样的事情可能是什么问题?
这是我用来管理作业的通用功能.我没有展示的是我传递给它的功能.它们很长,经常使用matplotlib,有时会启动一些shell命令,但我无法弄清楚失败的是什么共同点.
def runFunctionsInParallel(listOf_FuncAndArgLists):
"""
Take a list of lists like [function, arg1, arg2, ...]. Run those functions in parallel, wait for them all to finish, and return the list of their return values, in order.
"""
from multiprocessing import Process, Queue
def storeOutputFFF(fff,theArgs,que): #add a argument to function for assigning a queue
print 'MULTIPROCESSING: Launching %s in parallel '%fff.func_name
que.put(fff(*theArgs)) #we're putting return value into queue
print 'MULTIPROCESSING: Finished %s in parallel! '%fff.func_name
# We …Run Code Online (Sandbox Code Playgroud) 我写了一个Stack and Queue实现(基于Linked List).有一个堆栈(bigStack).例如,我分开bigStack(例如:stackA和stackB).我pop()是一个节点bigStack,我push()在stackA.以同样的方式,我push()在stackB.我想bigStack不要改变.因此我想克隆该bigStack对象.如何在C++中克隆对象?或者我的问题有另一种解决方案吗?
class Stack : public List {
public:
Stack() {}
Stack(const Stack& rhs) {}
Stack& operator=(const Stack& rhs) {};
~Stack() {}
int Top() {
if (head == NULL) {
cout << "Error: The stack is empty." << endl;
return -1;
} else {
return head->nosu;
}
}
void Push(int nosu, string adi, string …Run Code Online (Sandbox Code Playgroud) 我和我的朋友一直致力于一个大型项目,以便在python和PyGame中学习和娱乐.基本上它是一个小村庄的AI模拟.我们想要一个日/夜循环,所以我找到了一种利用numpy改变整个表面颜色的巧妙方法(特别是交叉渐变教程) - http://www.pygame.org/docs/tut/surfarray/SurfarrayIntro. HTML
我将它实现到代码中并且工作正常,但速度非常慢,比如<1 fps slow.所以我看看线程(因为我想最终添加它)并在队列中找到这个页面 - 在python中学习队列模块(如何运行它)
我花了大约15分钟制作一个基本系统,但是一旦我运行它,窗口关闭,它说
Exception in thread Thread-1 (most likely raised during interpreter shutdown):
Run Code Online (Sandbox Code Playgroud)
编辑:这就是它所说的全部,没有回溯错误
我不知道我做错了什么,但我想我错过了一些简单的事情.我在下面添加了代码的必要部分.
q_in = Queue.Queue(maxsize=0)
q_out = Queue.Queue(maxsize=0)
def run(): #Here is where the main stuff happens
#There is more here I am just showing the essential parts
while True:
a = abs(abs(world.degree-180)-180)/400.
#Process world
world.process(time_passed_seconds)
blank_surface = pygame.Surface(SCREEN_SIZE)
world.render(blank_surface) #The world class renders everything onto a blank surface
q_in.put((blank_surface, a))
screen.blit(q_out.get(), (0,0))
def DayNight():
while True:
blank_surface, a = …Run Code Online (Sandbox Code Playgroud) 我想我会重新提出我的问题
您应该在哪里使用BlockingQueue实现而不是简单队列实现?
至
BlockingQueue优于队列实现的优点/缺点是考虑速度,并发性或其他属性等方面,例如访问最后一个元素的时间.
我使用过这两种队列.我知道Blocking Queue通常用于并发应用程序.我正在编写简单的ByteBuffer池,我需要一个ByteBuffer对象的占位符.我需要最快,线程安全的队列实现.甚至像ArrayList这样的List实现也具有元素的持续访问时间.
任何人都可以讨论BlockingQueue与Queue vs List实现的优缺点吗?
目前我使用ArrayList来保存这些ByteBuffer对象.
我应该使用哪种数据结构来保存这些对象?
我怎样才能有效地实现一个列表数据结构,我可以在列表的头部和末尾有2个视图,这总是指向一个列表的尾部而没有昂贵的反向调用.即:
start x = []
end x = reverse start -- []
start1 = [1,2,3] ++ start
end start1 -- [3,2,1]
Run Code Online (Sandbox Code Playgroud)
end应该能够在不调用'reverse'的情况下执行此操作,而只是从列表的角度自动反向查看给定列表.如果我从连接开始创建新列表,那么同样应该成立.
在C#中有一些已定义的通用容器,它可以同时用作Stack和Queue吗?我只是希望能够将元素追加到末尾或队列的前面
谢谢
很抱歉这样一个愚蠢的问题,但Python文档令人困惑.
链接1:队列实施 http://docs.python.org/library/queue.html
它说那个队列有一个优先队列的构造.但我找不到如何实现它.
class Queue.PriorityQueue(maxsize=0)
Run Code Online (Sandbox Code Playgroud)
链接2:堆实现 http://docs.python.org/library/heapq.html
在这里,他们说我们可以使用heapq间接实现优先级队列
pq = [] # list of entries arranged in a heap
entry_finder = {} # mapping of tasks to entries
REMOVED = '<removed-task>' # placeholder for a removed task
counter = itertools.count() # unique sequence count
def add_task(task, priority=0):
'Add a new task or update the priority of an existing task'
if task in entry_finder:
remove_task(task)
count = next(counter)
entry = [priority, count, task]
entry_finder[task] = entry
heappush(pq, entry)
def remove_task(task): …Run Code Online (Sandbox Code Playgroud) 如果我对ThreadPool工作方式的理解是正确的,那么其目的之一就是限制可以在给定时间创建的进程中的工作线程数.例如,如果将MaxThreads设置为5,然后将QueueUserWorkItem调用30次,则将向ThreadPool发出30个请求,但这些请求中只有5个将由新线程提供服务,而其他25个请求将被添加到队列中并且随着先前的请求完成并且现有线程变得可用,一次服务一个.
但是,在下面的代码中,对Thread.Sleep(-1)的调用保证DoSomething()方法永远不会返回,这意味着当前线程永远不会对后续请求可用.
但是我对ThreadPool工作方式的理解不正确,因为如果它是正确的,下面的代码只打印数字0-4而不是0-29.
有人可以解释ThreadPool如何工作以及为什么下面的代码没有做我认为它应该做的事情?
static void DoSomething(object n)
{
Console.WriteLine(n);
Thread.Sleep(-1);
}
static void Main(string[] args)
{
ThreadPool.SetMaxThreads(5, 5);
for (int x = 0; x < 30; x++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(DoSomething), x);
}
Console.Read();
}
Run Code Online (Sandbox Code Playgroud) 我正在运行python 2.7.3,我注意到以下奇怪的行为.考虑这个最小的例子:
from multiprocessing import Process, Queue
def foo(qin, qout):
while True:
bar = qin.get()
if bar is None:
break
qout.put({'bar': bar})
if __name__ == '__main__':
import sys
qin = Queue()
qout = Queue()
worker = Process(target=foo,args=(qin,qout))
worker.start()
for i in range(100000):
print i
sys.stdout.flush()
qin.put(i**2)
qin.put(None)
worker.join()
Run Code Online (Sandbox Code Playgroud)
当我循环超过10,000或更多时,我的脚本会挂起worker.join().当循环仅达到1,000时,它工作正常.
有任何想法吗?
默认heapq是min队列实现,并想知道是否有最大队列选项?谢谢.
我尝试使用_heapify_max作为最大堆的解决方案,但是如何处理动态推/弹元素?似乎_heapify_max只能在初始化时使用.
import heapq
def heapsort(iterable):
h = []
for value in iterable:
heapq.heappush(h, value)
return [heapq.heappop(h) for i in range(len(h))]
if __name__ == "__main__":
print heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
Run Code Online (Sandbox Code Playgroud)
编辑,尝试_heapify_max似乎不适用于动态推/弹元素.我尝试两种方法输出相同,两个输出都是,[0,1,2,3,4,5,6,7,8,9].
def heapsort(iterable):
h = []
for value in iterable:
heapq.heappush(h, value)
return [heapq.heappop(h) for i in range(len(h))]
def heapsort2(iterable):
h = []
heapq._heapify_max(h)
for value in iterable:
heapq.heappush(h, value)
return [heapq.heappop(h) for i in range(len(h))]
if __name__ == "__main__":
print heapsort([1, 3, 5, …Run Code Online (Sandbox Code Playgroud)