什么是全球解释器锁,为什么它是一个问题?
围绕从Python中删除GIL已经产生了很多噪音,我想知道为什么这么重要.我自己从未编写过编译器或解释器,所以不要节俭细节,我可能需要他们理解.
是否有可能创建一个非守护进程的python池?我希望一个池能够调用一个内部有另一个池的函数.
我想要这个,因为deamon进程无法创建进程.具体来说,它会导致错误:
AssertionError: daemonic processes are not allowed to have children
Run Code Online (Sandbox Code Playgroud)
例如,考虑function_a具有运行的池的场景,该池具有运行function_b的池function_c.此函数链将失败,因为function_b正在守护进程中运行,并且守护进程无法创建进程.
请告诉我之间的差异ThreadPool,并Pool在multiprocessing模块.当我尝试我的代码时,这是我看到的主要区别:
from multiprocessing import Pool
import os, time
print("hi outside of main()")
def hello(x):
print("inside hello()")
print("Proccess id: ", os.getpid())
time.sleep(3)
return x*x
if __name__ == "__main__":
p = Pool(5)
pool_output = p.map(hello, range(3))
print(pool_output)
Run Code Online (Sandbox Code Playgroud)
我看到以下输出:
hi outside of main()
hi outside of main()
hi outside of main()
hi outside of main()
hi outside of main()
hi outside of main()
inside hello()
Proccess id: 13268
inside hello()
Proccess id: 11104
inside hello()
Proccess id: …Run Code Online (Sandbox Code Playgroud) python multiprocessing threadpool python-3.x python-multiprocessing
我正在尝试使用Python进行多处理器编程.Fibonacci例如,采用分治算法.程序流程的执行将像树一样分支并并行执行.换句话说,我们有一个嵌套并行性的例子.
从Java开始,我使用线程池模式来管理资源,因为程序可以非常快速地扩展并创建太多短期线程.可以通过实例化单个静态(共享)线程池 ExecutorService.
我希望Pool也一样,但看起来Pool对象不是全局共享的.例如,使用共享池multiprocessing.Manager.Namespace()将导致错误.
池对象不能在进程之间传递或被pickle
我有一个由两部分组成的问题:
from concurrent.futures import ThreadPoolExecutor
def fibonacci(n):
if n < 2:
return n
a = pool.submit(fibonacci, n - 1)
b = pool.submit(fibonacci, n - 2)
return a.result() + b.result()
def main():
global pool
N = int(10)
with ThreadPoolExecutor(2**N) as pool:
print(fibonacci(N))
main()
Run Code Online (Sandbox Code Playgroud)
Java的
public class FibTask implements Callable<Integer> {
public static ExecutorService pool = Executors.newCachedThreadPool();
int …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Python 2.7中的Queue.Queue实现多线程生产者 - 消费者模式.我试图找出如何使消费者,即工人线程,一旦完成所有必要的工作,停止.
请参阅Martin James对此答案的第二条评论:https://stackoverflow.com/a/19369877/1175080
发送'我完成'任务,指示池线程终止.任何获得此类任务的线程都会重新排队,然后自杀.
但这对我不起作用.例如,请参阅以下代码.
import Queue
import threading
import time
def worker(n, q):
# n - Worker ID
# q - Queue from which to receive data
while True:
data = q.get()
print 'worker', n, 'got', data
time.sleep(1) # Simulate noticeable data processing time
q.task_done()
if data == -1: # -1 is used to indicate that the worker should stop
# Requeue the exit indicator.
q.put(-1)
# Commit suicide.
print 'worker', n, 'is …Run Code Online (Sandbox Code Playgroud) python concurrency multithreading producer-consumer python-2.7
想象一下以下课程:
Class Object(threading.Thread):
# some initialisation blabla
def run(self):
while True:
# do something
sleep(1)
class Checker():
def check_if_thread_is_alive(self):
o = Object()
o.start()
while True:
if not o.is_alive():
o.start()
Run Code Online (Sandbox Code Playgroud)
我想重新启动线程,以防它死了.这不起作用.因为线程只能启动一次.第一个问题.为什么是这样?
据我所知,我必须重新创建每个实例Object并调用start()再次启动线程.在复杂Object的情况下,这不是很实用.我要读取旧的当前值Object,创建一个新值并使用旧值在新对象中设置参数.第二个问题:这可以通过更智能,更简单的方式完成吗?
我试图迭代超过100,000个图像并捕获一些图像功能,并将所得的dataFrame作为pickle文件存储在磁盘上。
不幸的是,由于RAM的限制,我被迫将图像分成20,000个大块并对其进行操作,然后再将结果保存到磁盘上。
在开始循环以处理下一个20,000图像之前,下面编写的代码应该保存20,000图像的结果数据框。
但是-这似乎没有解决我的问题,因为在第一个for循环结束时内存没有从RAM中释放
因此,在处理第50,000条记录时,该程序由于内存不足错误而崩溃。
在将对象保存到磁盘并调用垃圾收集器后,我尝试删除这些对象,但是RAM使用率似乎并未下降。
我想念什么?
#file_list_1 contains 100,000 images
file_list_chunks = list(divide_chunks(file_list_1,20000))
for count,f in enumerate(file_list_chunks):
# make the Pool of workers
pool = ThreadPool(64)
results = pool.map(get_image_features,f)
# close the pool and wait for the work to finish
list_a, list_b = zip(*results)
df = pd.DataFrame({'filename':list_a,'image_features':list_b})
df.to_pickle("PATH_TO_FILE"+str(count)+".pickle")
del list_a
del list_b
del df
gc.collect()
pool.close()
pool.join()
print("pool closed")
Run Code Online (Sandbox Code Playgroud) 我试图boilerpipe用Python 运行multiprocessing.这样做是为了解析来自多个来源的RSS源.问题是它在处理一些链接后挂在其中一个线程中.如果我删除池并在循环中运行它,整个流程都有效.
这是我的多处理代码:
proc_pool = Pool(processes=4)
for each_link in data:
proc_pool.apply_async(process_link_for_feeds, args=(each_link, ), callback=store_results_to_db)
proc_pool.close()
proc_pool.join()
Run Code Online (Sandbox Code Playgroud)
这是我boilerpipe在里面调用的代码process_link_for_feeds():
def parse_using_bp(in_url):
extracted_html = ""
if ContentParser.url_skip_p.match(in_url):
return extracted_html
try:
extractor = Extractor(extractor='ArticleExtractor', url=in_url)
extracted_html = extractor.getHTML()
del extractor
except BaseException as e:
print "Something's wrong at Boilerpipe -->", in_url, "-->", e
extracted_html = ""
finally:
return extracted_html
Run Code Online (Sandbox Code Playgroud)
我对它悬挂的原因一无所知.proc_pool代码中有什么问题吗?
我需要运行int f(int i)带有10_000参数的函数,由于I / O时间的原因,该过程大约需要1秒钟才能执行。
在Python之类的语言中,我可以使用线程(或者async/await,我知道,但稍后再讨论)来并行化此任务。
如果我希望始终有10个正在运行的线程,并在它们之间分配任务,则可以使用ThreadingPool:
def f(p):
x = [...]
return x
p = ThreadPool()
xs = p.map(f, range(10_000))
Run Code Online (Sandbox Code Playgroud)
但是它是如何工作的呢?如果我想使用类似的东西来实现,比如说NodeJS和f = http("www.google.com", callback),我应该从哪里开始呢?解决此类问题的算法是什么?
同样,我想同时获得10个请求,当一个请求完成时,下一个请求应该开始。
queue = ["www.google.com", "www.facebook.com"]
var f = function(url) {
http.get(url, (e) => {
const newUrl = queue.pop();
f(newUrl);
});
};
for (var i = 0; i < 10; i++) {
f(queue.pop());
}
Run Code Online (Sandbox Code Playgroud) ThreadPoolExecutorfromconcurrent.futures和ThreadPoolfrom有区别吗multiprocessing.dummy?本文建议将ThreadPool“线程”(任务)查询到CPU的不同“线程”上。是否concurrent.futures做同样的事情或者“线程”(任务)查询CPU的单个“线程”?
python ×9
python-2.7 ×3
threadpool ×3
python-3.x ×2
asynchronous ×1
boilerpipe ×1
concurrency ×1
gil ×1
node.js ×1
pandas ×1
pool ×1