我想在TLS中存储日志记录上下文信息,以便我可以在入口点设置一个值,并在所有结果栈中提供该值.这项工作很好,但我也使用TPL和ThreadPool.然后问题就变成了如何将TLS数据迁移到其他线程.我自己可以做到这一切,但后来我失去了像Parallel.For这样的好方法.
使用TPL时是否有某种方法可以复制TLS?当它获得await功能时,这也将适用于C#.
谢谢,埃里克
.net asynchronous threadpool task-parallel-library thread-local-storage
我正在开发一个.NET 4应用程序,它需要后端工作线程才能运行.该主题主要包含以下代码:
while (true) {
//Check stuff in database
//Do stuff
//write to database / filesystem
Thread.sleep(60000)
}
Run Code Online (Sandbox Code Playgroud)
ASP.NET应用程序只是数据库的前端.
我的问题在于放置这个工作循环的最佳位置.看来我的两个选择是(1)将它从Application_Start方法中分离出来,然后让它运行,或者(2)将它捆绑在一个单独的进程中(Windows服务?)
(1)显然需要ASP.NET代码中的一些逻辑来检查它是否仍在运行,因为IIS可能会将其终止.整个应用程序逻辑在一个易于部署的软件包中也非常简洁.(2)更加隔离,但感觉更麻烦.
什么是最好的方法?
我们正在使用Glassfish 3.0.1,并且经历了很长的响应时间; 对于25%的POST/PUT请求,大约5分钟,当响应返回时,前置负载平衡器已经超时.
我的理论是请求排队并等待可用的线程.
我认为这是因为访问日志显示请求需要几秒钟才能完成,但请求执行的时间比我预期的晚了五分钟.
有没有人对调试线程池的内容有任何建议?或者他们的最佳设置是什么?
是否需要定期进行线程转储,或者一次性转储是否足够?
我有一个简单的客户端应用程序,它以低吞吐量从网络接收字节缓冲区.这是代码:
private static readonly HashSet<int> _capturedThreadIds = new HashSet<int>();
private static void RunClient(Socket socket)
{
var e = new SocketAsyncEventArgs();
e.SetBuffer(new byte[10000], 0, 10000);
e.Completed += SocketAsyncEventsArgsCompleted;
Receive(socket, e);
}
private static void Receive(Socket socket, SocketAsyncEventArgs e)
{
var isAsynchronous = socket.ReceiveAsync(e);
if (!isAsynchronous)
SocketAsyncEventsArgsCompleted(socket, e);
}
private static void SocketAsyncEventsArgsCompleted(object sender, SocketAsyncEventArgs e)
{
if (e.LastOperation != SocketAsyncOperation.Receive || e.SocketError != SocketError.Success || e.BytesTransferred <= 0)
{
Console.WriteLine("Operation: {0}, Error: {1}, BytesTransferred: {2}", e.LastOperation, e.SocketError, e.BytesTransferred);
return;
}
var …Run Code Online (Sandbox Code Playgroud) 我想进入春季多线程,我几乎没有问题.
我在ThreadRating类中有runnable方法.现在我不确定使用它的最佳方式.
选项1我发现:
private void updateRating() {
ExecutorService executor = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) { // test
// thread part
Runnable worker = new ThreadRating(path, i, products.get(i), dao, fileHandler);
executor.execute(worker);
}
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
log.error("There was an error when ending threads");
System.exit(1);
}
System.out.println("Finished all threads");
}
Run Code Online (Sandbox Code Playgroud)
这似乎运行良好.在for循环之后,它等待直到线程完成并结束.
我试过第二个选项
private TaskExecutor taskExecutor;
public UpdateBO(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
private void updateRating() {
for (int i …Run Code Online (Sandbox Code Playgroud) 我有一个托管为Azure云服务的应用程序(Web API,.NET 4.5).
Vm大小和4个实例.
当我使用新文件监视应用程序时,我发现 ExecuteRequestHandler占用总响应时间的近80%并且使应用程序响应非常慢.
为了提高应用程序的性能,我希望减少ExecuteRequestHandler所花费的时间.
我试图在互联网上搜索.但除了以下链接之外没有得到任何帮助.
ExecuteRequestHandler大部分时间都在使用
以上链接没有多大帮助.
任何关于以下两点的帮助都非常感谢.
1)如何减少ExecuteRequestHandler所用的时间
2)如何增加Web角色的线程池大小.
我有一个脚本,随机生成一组数据并训练几个分类器,以相互比较它们(它非常类似于http://scikit-learn.org/stable/auto_examples/classification/plot_classifier_comparison.html):
from itertools import product
import numpy as np
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB, MultinomialNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split
names = ["Linear SVM", "Decision Tree",
"Random Forest", "AdaBoost", "Naive Bayes", "Linear Discriminant Analysis",
"Quadratic Discriminant Analysis"]
def griddy_mcsearchface(num_samples, num_feats, num_feats_to_remove):
classifiers = [
SVC(kernel="linear", C=0.025),
DecisionTreeClassifier(max_depth=5),
RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
AdaBoostClassifier(), …Run Code Online (Sandbox Code Playgroud) 当我运行我的代码时:
nb workers = 12
I'm i : 0
HELLO I'm func1
BYE I'm func2
terminate called after throwing an instance of 'std::system_error'
what(): Invalid argument
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
抛出“std::system_error”实例后调用终止
What():无效参数
#ifndef CPP_PLAZZA_EXAMPLE_H
#define CPP_PLAZZA_EXAMPLE_H
#include <thread>
#include <vector>
#include <list>
#include <memory>
#include <functional>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <iterator>
#include <tuple>
class ThreadPool
{
public:
ThreadPool(size_t numThreads);
virtual ~ThreadPool();
void executeJob(std::function<void()> job, std::function<void()> notificationJob);
void wait_for_done();
private:
void loop();
std::pair<std::function<void()>, std::function<void()> > getNextJob();
std::vector<std::thread> m_workers;
std::list<std::pair<std::function<void()>, …Run Code Online (Sandbox Code Playgroud) 是否可以像health在“主”应用程序的单独线程池中那样处理执行器请求?
我为什么要问?我有一个应用程序,有时可能会用完所有可用线程,并且 Kubernetes 运行状况检查由于线程不可用来计算运行状况端点请求而失败。
我想确保无论应用程序承受多少负载,都会处理每个运行状况请求。
我正在考虑为执行器定义一个单独的线程池来操作,但我不知道如何做到这一点。
我有一个基于Tokio的 Rust 异步服务器运行时。它必须同时处理对延迟敏感的 I/O 密集型请求和大量 CPU 密集型请求。
我不想让 CPU 密集型任务垄断 Tokio 运行时并使 I/O 密集型任务饿死,所以我想将 CPU 密集型任务卸载到专用的、隔离的线程池(隔离是这里的关键,所以spawn_blocking/block_in_place在一个共享线程池上是不够的)。如何在 Tokio 中创建这样的线程池?
启动两个运行时的幼稚方法会遇到错误:
线程“tokio-runtime-worker”因“无法从运行时内启动运行时”而恐慌。发生这种情况是因为一个函数(如
block_on)试图在当前线程被用于驱动异步任务时阻塞当前线程。
use tokio; // 0.2.20
fn main() {
let mut main_runtime = tokio::runtime::Runtime::new().unwrap();
let cpu_pool = tokio::runtime::Builder::new().threaded_scheduler().build().unwrap();
let cpu_pool = cpu_pool.handle().clone(); // this is the fix/workaround!
main_runtime.block_on(main_runtime.spawn(async move {
cpu_pool.spawn(async {}).await
}))
.unwrap().unwrap();
}
Run Code Online (Sandbox Code Playgroud)
Tokio 可以允许两个独立的运行时吗?有没有更好的方法在 Tokio 中创建隔离的 CPU 池?
threadpool ×10
.net ×2
c# ×2
java ×2
spring ×2
asp.net ×1
asynchronous ×1
asyncsocket ×1
c++ ×1
c++11 ×1
glassfish ×1
iis-7.5 ×1
numpy ×1
python ×1
rust ×1
rust-tokio ×1
scikit-learn ×1
spring-boot ×1
winsock ×1