如何进行多线程调用Web服务? [英] How to do multithreading for calling web service ?

查看:82
本文介绍了如何进行多线程调用Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要在最短的时间内从C#windows服务拨打100,000个Web服务,并获得更新我的数据库的响应。



应用程序托管在具有高性能的服务器上,我目前正在使用Parallel.For来进行线程化。但是,该应用程序每秒只能进行大约30次调用,CPU利用率仅为15%



我需要知道是否有更好的线程技术以及为什么我的应用程序没有利用服务器处理能力,只能使用15%?



我尝试过:



我尝试过以下但没有什么好转

< connectionmanagement>

< add address =*maxconnection = 2000>

Hi,

I need to make 100,000 call to a web service from C# windows service in the shortest possible time and get the response to update my database.

The application is hosted on a server with high capabilities and I'm currently using "Parallel.For" to do the threading. However, The application only makes around 30 call per second and the CPU utilization is only 15%

I need to know if there is a better threading technique and why my application is not utilizing and benefits from the server processing power and only uses 15% ?

What I have tried:

I have tried the below but nothing get better
<connectionmanagement>
<add address="*" maxconnection="2000">

推荐答案

简单来说,并行库将使用与CPU中的核心一样多的线程。如果你有一个带有超线程的4核CPU,你就有8个逻辑核心。默认情况下,Parallel一次最多可以从线程池中运行8个线程。



现在,你的CPU处于15%,因为这些线程都被阻塞了通过I / O绑定操作。所有8个线程都花费大部分时间等待Web请求完成。



你可以运行更多的线程,因为你有CPU资源只是坐在那里闲置,但是旋转线程是一项非常昂贵的操作。这需要花费很多时间,大约200,000个CPU周期来旋转一个,大约6,000个CPU周期将其拆除。记忆也是一个问题。你旋转的每个线程也会占用1MB的内存用于堆栈空间。旋转1000个线程,你也可以吃1GB的内存,而且没有线程执行任何操作!



上下文切换也是一个问题。如果在8核CPU上启动1,000个线程,则任何时间点只能运行8个线程。您的线程最终与所有其他进程(包括Windows)在系统中运行的数千个其他线程共享CPU。上下文切换是核心从执行一个线程切换到另一个线程的地方。这也是一种昂贵的,需要大约8,000个CPU周期。



在问题上投掷线程并不是获得性能的银弹。您必须知道导致性能问题的原因,瓶颈是什么,您正在做什么以及如何有效地将线程性能降至最低。



开始阅读此处 [ ^ ]。
In simplified terms, the Parallel library will use as many threads as you have cores in your CPU. If you've got a 4 core CPU with Hyperthreading, you've got 8 logical cores. By default, Parallel will run up to 8 threads out of the thread pool at a time.

Now, your CPU is sitting at 15% because those threads are all blocked by I/O bound operations. All 8 threads are spending most of their time sitting around waiting for a web request to complete.

You could run lots more threads, because you have CPU resources just sitting there idle, but spinning up a thread is a very expensive operation. It takes a lot of time, roughly 200,000 CPU cycles to spin one up and about 6,000 to tear it down. Memory is also an issue. Each thread you spin up will also eat 1MB of memory for stack space. Spin up 1,000 threads and you also eat 1GB of memory, and that's without the thread even executing anything!

Context switching is also a problem. If you spin up 1,000 threads on an 8 core CPU, only 8 threads can run at any point in time. Your threads end up sharing the CPU with the thousands of the other threads running in the system from all other process, including Windows. A context switch is where a core switches from executing one thread to another. This is also kind of expensive, taking about 8,000 CPU cycles.

Throwing threads at a problem is not the "silver bullet" to gain performance. You have to know what's causing your performance problem, what the bottlenecks are, what you're doing about it and how to do it efficiently to keep the performance hits of threading to a minimum.

Start reading here[^].


Parallel.For不是神奇。您的问题背后的问题可能与您的网络速度有关。也是Parallel.For仅适用于发布模式。在调试模式下,它不会执行任何并行操作。
Parallel.For isn't magical. The issue behind your problem may be related to your network speed. Also Parallel.For only works in release mode. In debug mode it doesn't do any Parallel stuff.


这篇关于如何进行多线程调用Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆