我有一个带静态get属性的静态类,在这个属性中,我这样做:
// property body
{
// HttpContext.Current is NOT null
...
Parallel.ForEach(files, file =>
{
// HttpContext.Current is null
var promo = new Promotion();
...
});
...
// HttpContext.Current is NOT null
}
Run Code Online (Sandbox Code Playgroud)
在视图使用此属性之前,此静态类不会进行类型初始化.
问题是,Promotion的静态构造函数,初始化第一次new Promotion()被内创建Parallel.ForEach(),使用HttpContext.Current.何时promo在此范围内实例化Parallel.ForEach(),HttpContext.Current是null,new Promotion()因此导致异常.
HttpContext.Current静态get属性中不为null,因为在视图使用它之前不会调用它(因此有一个HttpContext.Current).
如果Promotion使用HttpContext.Current在它的实例,而不是它的静态成员,我很可能只是传递HttpContext.Current到new Promotion()构造函数:
var context = HttpContext.Current;
Parallel.ForEach(files, file =>
{
var promo = …Run Code Online (Sandbox Code Playgroud) 它们都是一回事吗?只看几何中的并行或并行意味着什么,我会定义说不:
在几何中,如果它们在单个点相交,则称两条或更多条线是并发的.
和
平面中不相交或相交的两条线称为平行线.
再次,在编程中,它们是否具有相同的含义?如果是的话......为什么?
谢谢
我可以访问一台可以访问10个内核的机器 - 我想实际使用它们.我习惯在自己的机器上做的事情是这样的:
for f in *.fa; do
myProgram (options) "./$f" "./$f.tmp"
done
Run Code Online (Sandbox Code Playgroud)
我有10个文件我想这样做 - 让我们称之为blah00.fa,blah01.fa,... blah09.fa.
这种方法的问题是myProgram一次只使用1个核心,并且在多核机器上这样做我一次使用1个核心10次,所以我不会使用我的mahcine来它的最大能力.
我怎么能改变我的脚本,以便它同时运行我的所有10个.fa文件?我看着在多个核心的bash中运行一个循环进程,但我无法从中获取命令来完成我想要的操作.
我正在使用asp.net mvc,我想在他到达网站的主页时从数据库中缓存一些关于用户的数据.因此,当用户请求主页时,我想调用异步方法,该方法进行数据库调用和缓存数据.
这样做的任何例子都会很棒.
我正在使用concurrent.futures来实现多处理.我得到一个队列.全错误,这是奇怪的,因为我只分配10个工作.
A_list = [np.random.rand(2000, 2000) for i in range(10)]
with ProcessPoolExecutor() as pool:
pool.map(np.linalg.svd, A_list)
Run Code Online (Sandbox Code Playgroud)
错误:
Exception in thread Thread-9:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py", line 921, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py", line 869, in run
self._target(*self._args, **self._kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/concurrent/futures/process.py", line 251, in _queue_management_worker
shutdown_worker()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/concurrent/futures/process.py", line 209, in shutdown_worker
call_queue.put_nowait(None)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/queues.py", line 131, in put_nowait
return self.put(obj, False)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/queues.py", line 82, in put
raise Full
queue.Full
Run Code Online (Sandbox Code Playgroud) python parallel-processing multiprocessing concurrent.futures
我有30家子公司,每家公司都实施了他们的网络服务(使用不同的技术).
我需要实现一个Web服务来聚合它们,例如,所有子公司Web服务都有一个带有名称的Web方法,GetUserPoint(int nationalCode)我需要实现我的Web服务,它将调用所有这些并收集所有响应(例如总和)分数).
这是我的基类:
public abstract class BaseClass
{ // all same attributes and methods
public long GetPoint(int nationalCode);
}
Run Code Online (Sandbox Code Playgroud)
对于每个子公司Web服务,我实现了一个继承此基类并定义自己的GetPoint方法的类.
public class Company1
{
//implement own GetPoint method (call a web service).
}
Run Code Online (Sandbox Code Playgroud)
至
public class CompanyN
{
//implement own GetPoint method (call a web service).
}
Run Code Online (Sandbox Code Playgroud)
所以,这是我的网络方法:
[WebMethod]
public long MyCollector(string nationalCode)
{
BaseClass[] Clients = new BaseClass[] { new Company1(),//... ,new Company1()}
long Result = 0;
foreach (var item in Clients)
{
long …Run Code Online (Sandbox Code Playgroud) 我看到两种指定超时的方法concurrent.futures.
as_completed()wait()两种方法都处理N运行期货.
我想为每个未来指定一个单独的超时.
使用案例:
我该如何处理concurrent.futures?或者这个库不是正确的工具吗?
比方说,我有一个List<Integer> ints = new ArrayList<>();,我想将值添加到它,并使用比较并行执行的结果forEach()和Collectors.toList().
首先,我将一些来自顺序IntStream和forEach的值添加到此列表中:
IntStream.range(0,10).boxed().forEach(ints::add);
Run Code Online (Sandbox Code Playgroud)
我得到了正确的结果:
ints ==> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
现在我.clear()在列表中并行执行相同的操作:
IntStream.range(0,10).parallel().boxed().forEach(ints::add);
Run Code Online (Sandbox Code Playgroud)
现在由于多线程,我得到了错误的结果:
ints ==> [6, 5, 8, 9, 7, 2, 4, 3, 1, 0]
Run Code Online (Sandbox Code Playgroud)
现在我切换到收集相同的整数流:
IntStream.range(0,10).parallel().boxed().collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我得到了正确的结果:
ints ==> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
问题:
为什么两个并行执行产生不同的Collector结果?为什么产生正确的结果?
如果forEach产生随机结果,也Collector应该.我没有指定任何排序,我认为他在内部添加到我手动使用的列表forEach.由于他并行执行,因此他的add方法应该以未指定的顺序获取值.测试完成了JShell.
编辑:这里没有重复.我理解相关的问题.收集器为什么产生正确的结果?如果他将产生另一个随机结果,我不会问.
我正在尝试并行化管道。在管道中有一个 tidyr 命令(“tidyr::complete”)。一旦并行运行,这就会分解代码,因为无法识别对象类。
dplyr 中是否有替代方法可以完成?
library(dplyr)
library(tidyr)
library(zoo)
test <- tibble(year=c(1,2,3,4,5,5,1,4,5),
var_1=c(1,1,1,1,1,1,2,2,2),
var_2=c(1,1,1,1,1,2,3,3,3),
var_3=c(0,5,NA,15,20,NA,1,NA,NA))
max_year <- max(test$year,na.rm = T)
min_year <- min(test$year,na.rm = T)
Run Code Online (Sandbox Code Playgroud)
串行
test_serial <- test %>%
group_by(var_1,var_2) %>%
complete(var_1, year = seq(min_year,max_year)) %>%
mutate(
var_3 = na.approx(var_3,na.rm = FALSE),
var_3 = if(all(is.na(var_3))) NA else na.spline(var_3,na.rm = FALSE))
Run Code Online (Sandbox Code Playgroud)
并行(失败)
devtools::install_github("hadley/multidplyr")
library(multidplyr)
cl <- new_cluster(2)
cluster_copy(cl, c("test","max_year","min_year"))
cluster_library(cl, c("dplyr","tidyr","zoo"))
test_parallel <- test %>% group_by(var_1,var_2) %>% partition(cl)
test_parallel <- test_parallel %>%
dplyr::group_by(var_1,var_2) %>%
tidyr::complete(var_1, year = seq(min_year,max_year)) %>%
dplyr::mutate( …Run Code Online (Sandbox Code Playgroud) 我正在尝试按照此处的教程在我的图像集上使用预先训练的模板:https : //pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html
只有当我运行我的代码并且控制台锁定时,我总是得到这个“错误”:
[W ParallelNative.cpp:206] Warning: Cannot set number of intraop threads after parallel work has started or after set_num_threads call when using native parallel backend (function set_num_threads)
预先感谢您的帮助,
c# ×3
concurrency ×2
python ×2
asp.net ×1
asp.net-mvc ×1
asynchronous ×1
bash ×1
cnn ×1
cpu ×1
dplyr ×1
gpu ×1
java ×1
java-8 ×1
java-stream ×1
linux ×1
multidplyr ×1
pytorch ×1
r ×1
task ×1
terminology ×1