小编pas*_*oal的帖子

Python性能 - 最佳并行方法

我正在实现一个Python脚本,需要在不到5秒的时间内并行发送1500多个数据包.

简而言之,我需要的是:

def send_pkts(ip):
    #craft packet
    while True:
        #send packet
        time.sleep(randint(0,3))

for x in list[:1500]:
    send_pkts(x)
    time.sleep(randint(1,5))
Run Code Online (Sandbox Code Playgroud)

我尝试过简单的单线程,多线程,多处理和多处理+多线程表单,并遇到以下问题:

  1. 简单的单线程:" for delay"似乎会破坏"5秒"依赖.
  2. 多线程:由于Python GIL的限制,我认为我无法实现我的目标.
  3. 多处理:这是最好的方法似乎有效.但是,由于进程数量过多,我运行脚本的VM会冻结(当然,1500进程正在运行).因此变得不切实际.
  4. 多处理+多线程:在这种方法中,我创建了更少的进程,每个进程调用一些线程(假设:10进程每个调用150个线程).很明显,VM没有像3号方法一样快速冻结,但是我能达到的最多"并发数据包发送"是~800.GIL限制?VM限制?在这次尝试中,我也尝试使用Process Pool,但结果类似.

有没有更好的方法可以用来完成这项任务?

[1]编辑1:

 def send_pkt(x):
     #craft pkt
     while True:
         #send pkt
         gevent.sleep(0)

 gevent.joinall([gevent.spawn(send_pkt, x) for x in list[:1500]])
Run Code Online (Sandbox Code Playgroud)

[2]编辑2(gevent monkey-patching):

from gevent import monkey; monkey.patch_all()

jobs = [gevent.spawn(send_pkt, x) for x in list[:1500]]
gevent.wait(jobs)
#for send_pkt(x) check [1]
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:"ValueError:filedescriptor超出了select()"范围.所以我检查了我的系统ulimit(软和硬都是最大值:65536).之后,我检查了它与Linux 上的select()限制(最多1024 fds)有关.请检查:http://man7.org/linux/man-pages/man2/select.2.html(BUGS部分) - 为了解决这个问题,我应该使用poll()(http://man7.org/linux/ man-pages/man2/poll.2.html)相反.但是使用poll() …

python performance multithreading multiprocessing gil

9
推荐指数
1
解决办法
705
查看次数

Nodej似乎不起作用; 然而,npm确实有效

一周前我正在运行Node.js和Node-RED(取决于Node.js).我的系统是Windows 8.1 64位.

不过,今天我遇到了一个问题:

通常,我转到node-red文件夹,然后运行节点red.js. 然后,令人惊讶的是我从提示中得到以下消息:


Node Commands

Syntax:
    node {operator} [options] [arguments]

Parameters:
        /? or /help   - Display this help message.
        list          - List nodes or node history or the cluster
        listcores     - List cores on the cluster
        view          - View properties of a node
        online        - Set nodes or node to online state
        offline       - Set nodes or node to offline state
        pause         - Pause node [deprecated]
        resume        - Resume node [deprecated]

For more information about HPC command-line …
Run Code Online (Sandbox Code Playgroud)

windows node.js npm node-red

4
推荐指数
1
解决办法
1078
查看次数