什么是列表方法之间的差异append()和extend()?
在Python多处理库中,是否有pool.map的变体支持多个参数?
text = "test"
def harvester(text, case):
X = case[0]
text+ str(X)
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=6)
case = RAW_DATASET
pool.map(harvester(text,case),case, 1)
pool.close()
pool.join()
Run Code Online (Sandbox Code Playgroud) 我正在使用Python 3.5.2
我有两个清单
所以,我必须循环750,000个句子并执行大约20,000次替换,但是只有我的话实际上是"单词"并且不是更大字符串的一部分.
我是通过预先编译我的文字来做到这一点的,这样它们就被\b元字符所包围
compiled_words = [re.compile(r'\b' + word + r'\b') for word in my20000words]
Run Code Online (Sandbox Code Playgroud)
然后我循环我的"句子"
import re
for sentence in sentences:
for word in compiled_words:
sentence = re.sub(word, "", sentence)
# put sentence into a growing list
Run Code Online (Sandbox Code Playgroud)
这个嵌套循环每秒处理大约50个句子,这很好,但是处理我的所有句子仍需要几个小时.
有没有办法使用该str.replace方法(我认为更快),但仍然要求替换只发生在字边界?
或者,有没有办法加快re.sub方法?re.sub如果我的单词的长度大于句子的长度,我已经通过跳过来略微提高了速度,但这并没有太大的改进.
谢谢你的任何建议.
我发现在Python 3.4中,很少有用于多处理/线程的不同库:多处理与线程和asyncio.
但我不知道使用哪一个或是"推荐的".他们做同样的事情,还是不同?如果是这样,哪一个用于什么?我想编写一个在我的计算机中使用多核的程序.但我不知道我应该学习哪个图书馆.
python multithreading multiprocessing python-3.x python-asyncio
如何使用线程和子进程模块生成并行bash进程?当我启动线程时,第一个答案就在这里:如何在Python中使用线程?,bash进程按顺序而不是并行运行.
我正在用Python做一个机器学习项目,所以我必须做并行预测功能,我在我的程序中使用它.
from multiprocessing.dummy import Pool
from multiprocessing import cpu_count
def multi_predict(X, predict, *args, **kwargs):
pool = Pool(cpu_count())
results = pool.map(predict, X)
pool.close()
pool.join()
return results
Run Code Online (Sandbox Code Playgroud)
问题是我所有的CPU只加载了20-40%(总计为100%).我使用multiprocessing.dummy,因为我在pickling函数中遇到了多处理模块的问题.
使用时遇到此错误pool.map(funct, iterable):
AttributeError: __exit__
Run Code Online (Sandbox Code Playgroud)
否解释,只将堆栈跟踪到模块中的pool.py文件.
以这种方式使用:
with Pool(processes=2) as pool:
pool.map(myFunction, mylist)
pool.map(myfunction2, mylist2)
Run Code Online (Sandbox Code Playgroud)
我怀疑可挑选性可能存在问题(python需要pickle,或将列表数据转换为字节流)但我不确定这是否属实或是否如何调试.
编辑:产生此错误的新格式代码:
def governingFunct(list):
#some tasks
def myFunction():
# function contents
with closing(Pool(processes=2)) as pool:
pool.map(myFunction, sublist)
pool.map(myFunction2, sublist2)
Run Code Online (Sandbox Code Playgroud)
错误产生:
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Python运行一些简单的线程:
t1 = threading.Thread(analysis("samplequery"))
t1.start()
other code runs in here
t1.join()
Run Code Online (Sandbox Code Playgroud)
不幸的是我收到了错误:
"AssertionError:group参数现在必须为none"
我之前从未在Python中实现过线程,所以我有点不确定出了什么问题.有谁知道问题是什么?
我不确定它是否相关,但分析是从另一个文件导入的方法.
我也有一个跟进查询.Analysis返回一个字典,我将如何分配在原始方法中使用?
谢谢
在Bash中,可以通过追加在后台执行命令&.我怎么能在Python中做到这一点?
while True:
data = raw_input('Enter something: ')
requests.post(url, data=data) # Don't wait for it to finish.
print('Sending POST request...') # This should appear immediately.
Run Code Online (Sandbox Code Playgroud) 我是 dask 的新手,我发现拥有一个可以轻松实现并行化的模块真是太好了。我正在做一个项目,我可以在一台机器上并行化一个循环,正如你在这里看到的。但是,我想转移到dask.distributed. 我对上面的类应用了以下更改:
diff --git a/mlchem/fingerprints/gaussian.py b/mlchem/fingerprints/gaussian.py
index ce6a72b..89f8638 100644
--- a/mlchem/fingerprints/gaussian.py
+++ b/mlchem/fingerprints/gaussian.py
@@ -6,7 +6,7 @@ from sklearn.externals import joblib
from .cutoff import Cosine
from collections import OrderedDict
import dask
-import dask.multiprocessing
+from dask.distributed import Client
import time
@@ -141,13 +141,14 @@ class Gaussian(object):
for image in images.items():
computations.append(self.fingerprints_per_image(image))
+ client = Client()
if self.scaler is None:
- feature_space = dask.compute(*computations, scheduler='processes',
+ feature_space = dask.compute(*computations, scheduler='distributed',
num_workers=self.cores)
feature_space = OrderedDict(feature_space)
else:
stacked_features …Run Code Online (Sandbox Code Playgroud) python ×10
append ×1
dask ×1
extend ×1
list ×1
performance ×1
pickle ×1
python-3.x ×1
regex ×1
replace ×1
string ×1
subprocess ×1