csharp fetch和xhr异步

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp fetch和xhr异步相关的知识,希望对你有一定的参考价值。

var timeout = 1000;
var send_timeout = 3;
var url = 'http://localhost:22348/api/values'
url = 'http://127.0.0.1:3000/'
var responses = 0;
var requests = 0;
function go() {
 console.log('requests =', requests, ' responses =', responses);
 responses = requests = 0;
 setTimeout(go, timeout);
}
setTimeout(go, timeout); 

function sending() {
 fetch(url).then(_ => ++responses);
 requests++;
 setTimeout(sending, send_timeout);
}
setTimeout(sending, send_timeout);
import asyncio
import aiohttp

import requests
# import grequests



import time
from concurrent.futures import ProcessPoolExecutor
from concurrent.futures import ThreadPoolExecutor

from repeatingtimer import RepeatingTimer

from threading import Lock

res_count = 0;
req_count = 0;

def print_statistics(lock):
 global res_count, req_count
 print('requests =', req_count, ' responses =', res_count);
 res_count = req_count = 0;

def fetch(session, url, lock):
  # if lock.lcked():
    # print(lock)
  global res_count,req_count
  # with lock:
  req_count += 1
  # print('requesting ...')
  # grequests.get(url, hooks = {'response': inc})
  r = session.get(url)
  # print('got response')
  # with lock:
  res_count += 1

async def fetch_aiohttp(session, url, lock):
    global res_count, req_count
    req_count += 1
    async with session.get(url) as response:
        res_count += 1
        return await response.text()

async def main():
    lock = Lock()
    timer = RepeatingTimer(1, print_statistics, 0, lock)
            
    count = 10000
    loop = asyncio.get_event_loop()
    url = 'http://127.0.0.1:3000/'
    fs = []
    executor = ThreadPoolExecutor()
    # executor = ProcessPoolExecutor(count)
    
    timer.start()
    # timer.cancel()
    start_time = time.time()

    aiohttpFlag = True
    # aiohttpFlag = False

    if aiohttpFlag:
        session = aiohttp.ClientSession()
    else:
        session = requests.Session()
    # async with aiohttp.ClientSession() as session:
    for i in range(0, count):
        if aiohttpFlag:
            f = fetch_aiohttp(session, url, lock)
        else:
            f = loop.run_in_executor(executor, fetch, session, url, lock) 
        fs.append(f)
 
    print_statistics(lock)
    done, pending = await asyncio.wait(fs)

    total_time = (time.time() - start_time)*1000
    timer.cancel()
    if aiohttpFlag: await session.close()

    # print the rest ...
    print_statistics(lock)
    print_statistics(lock)
    

    # print stats
    print("--- %.2fms ---" % (total_time))
    print('done = ', len(done), 'pending = ' , len(pending))
    print('average res_count = ', 1000 * len(done) // total_time, ' per second')

    for f in done:
      break
      f.result().json()
 
# asyncio.run(main())
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
from __future__ import print_function
from threading import Timer

count = 0

def hello():
    global count
    count -= 1
    print('count =', count)

    # print("Hello World!")


class RepeatingTimer(object):

    def __init__(self, interval, f, max_count = 0, *args, **kwargs):
        self.interval = interval
        self.f = f
        self.max_count = max_count
        self.start_count = 0

        self.args = args
        self.kwargs = kwargs

        self.timer = None

    def callback(self):
        self.f(*self.args, **self.kwargs)
        self.start()

    def cancel(self):
        self.timer.cancel()

    def start(self):
        if self.max_count > 0:
            self.start_count += 1
            if self.start_count > self.max_count:
                return

        self.timer = Timer(self.interval, self.callback)
        self.timer.start()

if __name__ == '__main__':
    t = RepeatingTimer(0.001, hello, 10)
    t.start()
    
    for x in range(0,10):
        count += 1
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

function sleep(seconds) {
	const stoptime = new Date().getTime() + seconds * 1000;
	while (new Date().getTime() < stoptime)
		; // do nothing
}

var req_count = 0;
const emulate_work_timeout = 1000;

let tasks_queue = []

const server = http.createServer((req, res) => {
  // res.statusCode = 200;
  // res.setHeader('Content-Type', 'text/plain');
  // res.end('Hello World\n');
  // new Promise(resolve => setTimeout(_ => resolve(), emulate_work_timeout)).then(_ => res.end('200 OK'))
  
  // not emulating, now using task queue
  tasks_queue.push(req)
  res.end('200 OK')
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
})


// GET api/values
[HttpGet]
public bool Get()
{
    return true;
}

以上是关于csharp fetch和xhr异步的主要内容,如果未能解决你的问题,请参考以下文章

fetchaxios 和Ajax区别

fetch、ajax 和 xhr 之间的区别

分别使用 XHRjQuery 和 Fetch 实现 AJAX

javascript 从服务器请求数据(fetch,XHR和jQuery Ajax)

ajax与fetch的用法

Ajaxfetchaxios