我觉得这样做应该相当简单,但我终生无法找到解决方案......我想在与它所在的环境不同的环境中评估 R 函数。
我想要什么:
# A simple function
f <- function() {
x + 1
}
# Create an env and assign x <- 3
env <- new.env()
assign("x", 3, envir = env)
# Call f on env
call_on_env(f, env)
#> 4
Run Code Online (Sandbox Code Playgroud)
我最接近“ call_on_env()”的是:
# Quote call and evaluate
quo <- quote(f())
eval(quo, envir = env)
Run Code Online (Sandbox Code Playgroud)
不幸的是,上面的代码返回一个错误:Error in f() : object 'x' not found. 因此,然后...有没有办法让我评价f()的env?
编辑:我能发送f()到env,然后调用它,但这种叶子f()永久存在。对于上下文[见下文],我想与一些预加载的包并行调用该函数。 …
我正在开发一个程序,当我的艺术家在 Spotify 上有新音乐时,它会通过电子邮件发送给我。它通过在脚本运行时获取每个艺术家拥有的专辑数量并将结果与保存为 CSV 文件的前一天进行比较来实现这一点。
这涉及 API 调用以验证艺术家是否在 Spotify 上(我收到某些专辑不在 Spotify 上的错误),然后获取该艺术家的专辑数量。这些电话非常耗时,尤其是当我有近千名艺术家时。
我想知道如何并行化这些 API 调用或任何其他建议以加速整个程序。下面链接的是具有 API 调用的代码部分。提前感谢您的时间。
# given artist name returns all info related to artist
def get_artist_info(spotipy_instance, name):
results = spotipy_instance.search(q='artist:' + name, type='artist')
items = results['artists']['items']
if len(items) > 0:
return items[0]
else:
return None
# returns list of all albums given artist name
def get_artist_albums(spotipy_instance, artist):
albums = []
results = spotipy_instance.artist_albums(artist['id'], album_type='album')
albums.extend(results['items'])
while results['next']:
results = spotipy_instance.next(results)
albums.extend(results['items'])
seen = set() # to avoid …Run Code Online (Sandbox Code Playgroud) 我想知道,哪个更好地GridSearchCV( ..., n_jobs = ... )用于为模型选择最佳参数集,n_jobs = -1或者n_jobs使用大数字,
例如n_jobs = 30?
基于 Sklearn 文档:
n_jobs = -1意味着计算将在计算机的所有 CPU 上分派。
在我的 PC 上,我有一个 Intel i3 CPU,它有 2 个内核和 4 个线程,这是否意味着如果我设置了n_jobs = -1,它会隐式地等于n_jobs = 2?
python parallel-processing machine-learning scikit-learn parallelism-amdahl
我有一个 SSRS 报告,它有一个单一的数据源 - SSAS 表格立方体。
该报告有 15 个参数,它们从查询(数据集)中获取它们的值。
当用户打开报告时,每个参数都被填充,但每个查询执行都是序列化的(由 Profiler / Execution Log 确认)。每次执行最多需要 70 毫秒。因此,仅打开一个报告就需要 1,000-1,200 毫秒。
有没有办法并行填充报告参数?
注意
SSRS/SSAS 版本:2016,最新的 SP/CU、Ent & Dev
更新:如果我将数据源更改为 SQL Server,问题仍然存在,SSRS 不会并行执行查询(对于报表参数)。
sql-server parallel-processing parameters reporting-services
我开始学习multiprocessinginpython并且我注意到在主进程上执行相同的代码比在使用multiprocessing模块创建的进程中执行得快得多。
这是我的代码的简化示例,其中我首先执行代码main process并打印前 10 个计算的时间和总计算的时间。然后执行相同的代码new process(这是一个长时间运行的进程,我可以随时发送new_pattern)。
import multiprocessing
import random
import time
old_patterns = [[random.uniform(-1, 1) for _ in range(0, 10)] for _ in range(0, 2000)]
new_patterns = [[random.uniform(-1, 1) for _ in range(0, 10)] for _ in range(0, 100)]
new_pattern_for_processing = multiprocessing.Array('d', 10)
there_is_new_pattern = multiprocessing.Value('i', 0)
queue = multiprocessing.Queue()
def iterate_and_add(old_patterns, new_pattern):
for each_pattern in old_patterns:
sum = 0
for count in range(0, 10):
sum += …Run Code Online (Sandbox Code Playgroud) python parallel-processing multiprocessing long-running-processes
我以前没有使用过分布式计算,但我正在尝试将 mpi4py 集成到程序中,以便在计算集群上并行化 for 循环。
这是我想要做的伪代码:
for file in directory:
Initialize a class
Run class methods
Conglomerate results
我已经查看了堆栈溢出的所有内容,但找不到任何解决方案。有没有什么办法可以简单地使用mpi4py来做到这一点,或者有没有其他工具可以通过简单的安装和设置来做到这一点?
我试图了解如何在内存 sqlite3 中并行运行 django 测试。
我有具有该结构的 django 应用程序:
gbook
order
...
tests
__init__.py
test_a1.py
test_b1.py
utils.py
Run Code Online (Sandbox Code Playgroud)
test_a1.py 和 test_b1.py 包含相同的代码:
import time
from order import models
from .utils import BackendTestCase
class ATestCase(BackendTestCase):
def test_a(self):
time.sleep(1)
a = models.City.objects.count()
self.assertEqual(a, a)
class BTestCase(BackendTestCase):
def test_b(self):
time.sleep(1)
a = models.City.objects.count()
self.assertEqual(a, a)
Run Code Online (Sandbox Code Playgroud)
utils.py 是:
from django.test import TestCase, Client
from order import models
from django.conf import settings
from order.utils import to_hash
class BackendTestCase(TestCase):
fixtures = ['City.json', 'Agency.json']
def setUp(self):
self.client = Client() …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试实施Tensorflow 管道。事实上,我想用我的CPU加载数据,并使用我的GPU来运行图在同一时间。为了更好地理解正在发生的事情,我创建了一个非常简单的卷积网络:
import os
import h5py
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
sess= tf.InteractiveSession()
from tensorflow.python.client import timeline
import time
t1 = time.time()
class generator:
def __init__(self, file):
self.file = file
def __call__(self):
with h5py.File(self.file, 'r') as hf:
for im in hf["data"]:
yield tuple(im)
dataset = tf.data.Dataset().from_generator(generator('file.h5'),
output_types= tf.float32,
output_shapes=(tf.TensorShape([None,4,128,128,3])))
dataset = dataset.batch(batch_size=1000)
dataset = dataset.prefetch(10)
iter = dataset.make_initializable_iterator()
e1 = iter.get_next()
e1 = tf.reshape(e1, (-1, 128, 128, 3))
with tf.device('gpu'):
output = tf.layers.conv2d(e1[:150],200,(5,5))
output …Run Code Online (Sandbox Code Playgroud) queue parallel-processing multithreading timeline tensorflow
我使用 joblib 来并行化一个函数(使用多处理)。但是,这个函数返回 4 个值,但是当我从 Parallel 得到结果时,它只给了我 3 个值
from joblib import Parallel, delayed
import numpy as np
from array import array
import time
def best_power_strategy():
powerLoc = {0}
speedLoc = {1}
timeLoc = {2}
previousSpeedLoc = {3}
return powerLoc,speedLoc,timeLoc,previousSpeedLoc
if __name__ == "__main__":
realRiderName=['Rider 1', 'Rider 2', 'Rider 3']
powerLoc = {}
speedLoc = {}
timeLoc = {}
previousSpeedLoc = {}
powerLoc,speedLoc,timeLoc,previousSpeedLoc = Parallel(n_jobs=3)(delayed(best_power_strategy)() for rider in realRiderName)
print(powerLoc)
print(speedLoc)
print(timeLoc)
print(previousSpeedLoc)
Run Code Online (Sandbox Code Playgroud)
结果是:
ValueError: not enough values to unpack …Run Code Online (Sandbox Code Playgroud) python parallel-processing return-value multiprocessing joblib
使用从命令行Rscript调用时,我间歇性地收到以下错误mclapply:
Error in sendMaster(try(lapply(X = S, FUN = FUN, ...), silent = TRUE)) :
write error, closing pipe to the master
Run Code Online (Sandbox Code Playgroud)
如果我在 R Studio 或交互式 R 会话中运行完全相同的代码,则不会出现错误。这个错误会在非常大的作业的各种上下文中弹出,每个工作人员必须将非常大的对象返回给小作业。我也试过关闭prescheduling,但它仍然抛出错误。有时,如果我减少mc.cores参数中的线程数,它就会消失。我在 Ubuntu 18.04.1 上使用 Microsoft R Open。它也出现在 Ubuntu 16.04 上。我没有尝试过的一件事是在标准 R 而不是 MRO 中运行代码。
这是我的Rscript -e 'sessionInfo()':
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS
Matrix products: default
BLAS: /opt/microsoft/ropen/3.5.1/lib64/R/lib/libRblas.so
LAPACK: /opt/microsoft/ropen/3.5.1/lib64/R/lib/libRlapack.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] …Run Code Online (Sandbox Code Playgroud) python ×6
r ×2
django ×1
environment ×1
eval ×1
joblib ×1
mclapply ×1
microsoft-r ×1
mpi4py ×1
parameters ×1
queue ×1
return-value ×1
rlang ×1
rscript ×1
scikit-learn ×1
spotify ×1
sql-server ×1
tensorflow ×1
testing ×1
timeline ×1