支持python的分布式计算框架Ray详解

项目地址:https://github.com/ray-project/ray

1、简介

Ray为构建分布式应用程序提供了一个简单、通用的API。Ray是一种分布式执行框架,便于大规模应用程序和利用先进的机器学习库。

Ray通过以下方式完成这项任务:

为构建和运行分布式应用程序提供简单的原语。

使最终用户能够并行化单个机器代码,而代码更改很少到零。

在核心Ray之上包含大量应用程序、库和工具,以支持复杂的应用程序。

2、安装

安装方式比较简单: pip install ray==1.4.1

[root@node2 ~]# pip install 'ray[default]'
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting ray[default]
  Downloading https://mirrors.aliyun.com/pypi/packages/13/ec/f727ddd3fbcdc6102eace62c9d5dd9d9ad8112d40eeb7de8783676aca24d/ray-1.4.1-cp36-cp36m-manylinux2014_x86_64.whl (51.6 MB)
     |████████████████████████████████| 51.6 MB 269 kB/s
Collecting aiohttp
  Downloading https://mirrors.aliyun.com/pypi/packages/31/28/7b49246d7825c61e1a14306ab050e8eec48fcf5b009b19c8f6ea4e312467/aiohttp-3.7.4.post0-cp36-cp36m-manylinux2014_x86_64.whl (1.3 MB)
     |████████████████████████████████| 1.3 MB 121.5 MB/s
Requirement already satisfied: dataclasses in /usr/local/lib/python3.6/site-packages (from ray[default]) (0.8)
Collecting protobuf>=3.15.3
  Downloading https://mirrors.aliyun.com/pypi/packages/53/4e/e2db88d0bb0bda6a879eea62fddbaf813719ce3770d458bc5580512d9c95/protobuf-3.17.3-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.0 MB)
     |████████████████████████████████| 1.0 MB 136.1 MB/s
Collecting jsonschema
  Downloading https://mirrors.aliyun.com/pypi/packages/c5/8f/51e89ce52a085483359217bc72cdbf6e75ee595d5b1d4b5ade40c7e018b8/jsonschema-3.2.0-py2.py3-none-any.whl (56 kB)
     |████████████████████████████████| 56 kB 7.1 MB/s
Collecting aioredis
  Downloading https://mirrors.aliyun.com/pypi/packages/b0/64/1b1612d0a104f21f80eb4c6e1b6075f2e6aba8e228f46f229cfd3fdac859/aioredis-1.3.1-py3-none-any.whl (65 kB)
     |████████████████████████████████| 65 kB 6.3 MB/s
Collecting pydantic>=1.8
  Downloading https://mirrors.aliyun.com/pypi/packages/2b/7c/7d0b3f2d7959b7193018896db236ded165f9bca1bb75f46f4c32fa6f4f9d/pydantic-1.8.2-cp36-cp36m-manylinux2014_x86_64.whl (10.2 MB)
     |████████████████████████████████| 10.2 MB 420 kB/s
Collecting pyyaml
  Downloading https://mirrors.aliyun.com/pypi/packages/7a/5b/bc0b5ab38247bba158504a410112b6c03f153c652734ece1849749e5f518/PyYAML-5.4.1-cp36-cp36m-manylinux1_x86_64.whl (640 kB)
     |████████████████████████████████| 640 kB 19.1 MB/s
Collecting numpy>=1.16
  Downloading https://mirrors.aliyun.com/pypi/packages/14/32/d3fa649ad7ec0b82737b92fefd3c4dd376b0bb23730715124569f38f3a08/numpy-1.19.5-cp36-cp36m-manylinux2010_x86_64.whl (14.8 MB)
     |████████████████████████████████| 14.8 MB 296 kB/s
Collecting gpustat
  Downloading https://mirrors.aliyun.com/pypi/packages/b4/69/d8c849715171aeabd61af7da080fdc60948b5a396d2422f1f4672e43d008/gpustat-0.6.0.tar.gz (78 kB)
     |████████████████████████████████| 78 kB 7.3 MB/s
Collecting redis>=3.5.0
  Downloading https://mirrors.aliyun.com/pypi/packages/a7/7c/24fb0511df653cf1a5d938d8f5d19802a88cef255706fdda242ff97e91b7/redis-3.5.3-py2.py3-none-any.whl (72 kB)
     |████████████████████████████████| 72 kB 968 kB/s
Collecting aiohttp-cors
  Downloading https://mirrors.aliyun.com/pypi/packages/13/e7/e436a0c0eb5127d8b491a9b83ecd2391c6ff7dcd5548dfaec2080a2340fd/aiohttp_cors-0.7.0-py3-none-any.whl (27 kB)
Collecting py-spy>=0.2.0
  Downloading https://mirrors.aliyun.com/pypi/packages/9d/4d/1a9cbe9a0b543e6733cb38afe26451522a9ef8e4897b59e74cc76838f245/py_spy-0.3.7-py2.py3-none-manylinux1_x86_64.whl (3.1 MB)
     |████████████████████████████████| 3.1 MB 131 kB/s
Requirement already satisfied: requests in /usr/local/lib/python3.6/site-packages (from ray[default]) (2.24.0)
Collecting colorama
  Downloading https://mirrors.aliyun.com/pypi/packages/44/98/5b86278fbbf250d239ae0ecb724f8572af1c91f4a11edf4d36a206189440/colorama-0.4.4-py2.py3-none-any.whl (16 kB)
Collecting filelock
  Downloading https://mirrors.aliyun.com/pypi/packages/93/83/71a2ee6158bb9f39a90c0dea1637f81d5eef866e188e1971a1b1ab01a35a/filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting prometheus-client>=0.7.1
  Downloading https://mirrors.aliyun.com/pypi/packages/09/da/4e8471ff825769581593b5b84769d32f58e5373b59fccaf355d3529ad530/prometheus_client-0.11.0-py2.py3-none-any.whl (56 kB)
     |████████████████████████████████| 56 kB 4.2 MB/s
Collecting msgpack<2.0.0,>=1.0.0
  Downloading https://mirrors.aliyun.com/pypi/packages/0c/0d/b1d9d32d03ce38ba5e2a37fbae850afd4530a14cc441e8335f1865a03705/msgpack-1.0.2-cp36-cp36m-manylinux1_x86_64.whl (272 kB)
     |████████████████████████████████| 272 kB 127.6 MB/s
Requirement already satisfied: click>=7.0 in /usr/local/lib/python3.6/site-packages (from ray[default]) (7.1.2)
Collecting opencensus
  Downloading https://mirrors.aliyun.com/pypi/packages/18/59/12044123133d000f705383ad98579aeb0dd82d66b33a254a21b54bf0d6bb/opencensus-0.7.13-py2.py3-none-any.whl (127 kB)
     |████████████████████████████████| 127 kB 128.9 MB/s
Collecting grpcio>=1.28.1
  Downloading https://mirrors.aliyun.com/pypi/packages/eb/32/332a2bb0cee0d4040331952c5ef3d040d119a15b3c622b60bdcbe768bded/grpcio-1.39.0-cp36-cp36m-manylinux2014_x86_64.whl (4.3 MB)
     |████████████████████████████████| 4.3 MB 417 kB/s
Collecting colorful
  Downloading https://mirrors.aliyun.com/pypi/packages/b0/8e/e386e248266952d24d73ed734c2f5513f34d9557032618c8910e605dfaf6/colorful-0.5.4-py2.py3-none-any.whl (201 kB)
     |████████████████████████████████| 201 kB 113.9 MB/s
Requirement already satisfied: six>=1.5.2 in /usr/local/lib/python3.6/site-packages (from grpcio>=1.28.1->ray[default]) (1.15.0)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.6/site-packages (from pydantic>=1.8->ray[default]) (3.10.0.0)
Collecting multidict<7.0,>=4.5
  Downloading https://mirrors.aliyun.com/pypi/packages/a1/35/b22524d6b9cacfb4c5eff413a069bbc17c6ea628e54da5c6c989998ced5f/multidict-5.1.0-cp36-cp36m-manylinux2014_x86_64.whl (141 kB)
     |████████████████████████████████| 141 kB 120.6 MB/s
Collecting idna-ssl>=1.0
  Downloading https://mirrors.aliyun.com/pypi/packages/46/03/07c4894aae38b0de52b52586b24bf189bb83e4ddabfe2e2c8f2419eec6f4/idna-ssl-1.1.0.tar.gz (3.4 kB)
Collecting attrs>=17.3.0
  Downloading https://mirrors.aliyun.com/pypi/packages/20/a9/ba6f1cd1a1517ff022b35acd6a7e4246371dfab08b8e42b829b6d07913cc/attrs-21.2.0-py2.py3-none-any.whl (53 kB)
     |████████████████████████████████| 53 kB 3.9 MB/s
Requirement already satisfied: chardet<5.0,>=2.0 in /usr/local/lib/python3.6/site-packages (from aiohttp->ray[default]) (3.0.4)
Collecting yarl<2.0,>=1.0
  Downloading https://mirrors.aliyun.com/pypi/packages/da/08/52b26b44bce7b818b410aee37c5e424c9ea420c557bca97dc2adac29b151/yarl-1.6.3-cp36-cp36m-manylinux2014_x86_64.whl (293 kB)
     |████████████████████████████████| 293 kB 130.8 MB/s
Collecting async-timeout<4.0,>=3.0
  Downloading https://mirrors.aliyun.com/pypi/packages/e1/1e/5a4441be21b0726c4464f3f23c8b19628372f606755a9d2e46c187e65ec4/async_timeout-3.0.1-py3-none-any.whl (8.2 kB)
Requirement already satisfied: idna>=2.0 in /usr/local/lib/python3.6/site-packages (from idna-ssl>=1.0->aiohttp->ray[default]) (2.10)
Collecting hiredis
  Downloading https://mirrors.aliyun.com/pypi/packages/82/72/313fb6d30b7e413c8662447f9a0abe9105494ceb9266ecb08d442c14afbb/hiredis-2.0.0-cp36-cp36m-manylinux2010_x86_64.whl (84 kB)
     |████████████████████████████████| 84 kB 4.8 MB/s
Collecting nvidia-ml-py3>=7.352.0
  Downloading https://mirrors.aliyun.com/pypi/packages/6d/64/cce82bddb80c0b0f5c703bbdafa94bfb69a1c5ad7a79cff00b482468f0d3/nvidia-ml-py3-7.352.0.tar.gz (19 kB)
Collecting psutil
  Downloading https://mirrors.aliyun.com/pypi/packages/da/82/56cd16a4c5f53e3e5dd7b2c30d5c803e124f218ebb644ca9c30bc907eadd/psutil-5.8.0-cp36-cp36m-manylinux2010_x86_64.whl (291 kB)
     |████████████████████████████████| 291 kB 131.6 MB/s
Collecting blessings>=1.6
  Downloading https://mirrors.aliyun.com/pypi/packages/03/74/489f85a78247609c6b4f13733cbf3ba0d864b11aa565617b645d6fdf2a4a/blessings-1.7-py3-none-any.whl (18 kB)
Requirement already satisfied: setuptools in /usr/lib/python3.6/site-packages (from jsonschema->ray[default]) (39.2.0)
Collecting pyrsistent>=0.14.0
  Downloading https://mirrors.aliyun.com/pypi/packages/6c/19/1af501f6f388a40ede6d0185ba481bdb18ffc99deab0dd0d092b173bc0f4/pyrsistent-0.18.0-cp36-cp36m-manylinux1_x86_64.whl (117 kB)
     |████████████████████████████████| 117 kB 138.2 MB/s
Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.6/site-packages (from jsonschema->ray[default]) (4.3.0)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.6/site-packages (from importlib-metadata->jsonschema->ray[default]) (3.4.1)
Collecting opencensus-context==0.1.2
  Downloading https://mirrors.aliyun.com/pypi/packages/f1/33/990f1bd9e7ee770fc8d3c154fc24743a96f16a0e49e14e1b7540cc2fdd93/opencensus_context-0.1.2-py2.py3-none-any.whl (4.4 kB)
Collecting google-api-core<2.0.0,>=1.0.0
  Downloading https://mirrors.aliyun.com/pypi/packages/aa/51/629a31d4e7db2bbc0b0b789ac4034108c68647bea9cf5b54c8366df47b8e/google_api_core-1.31.0-py2.py3-none-any.whl (93 kB)
     |████████████████████████████████| 93 kB 1.6 MB/s
Collecting contextvars
  Downloading https://mirrors.aliyun.com/pypi/packages/83/96/55b82d9f13763be9d672622e1b8106c85acb83edd7cc2fa5bc67cd9877e9/contextvars-2.4.tar.gz (9.6 kB)
Collecting google-auth<2.0dev,>=1.25.0
  Downloading https://mirrors.aliyun.com/pypi/packages/d7/13/35b1e0a63e160ecad04985e60339382e48e6020d22f8328a2ab47226c910/google_auth-1.33.1-py2.py3-none-any.whl (152 kB)
     |████████████████████████████████| 152 kB 127.7 MB/s
Collecting packaging>=14.3
  Downloading https://mirrors.aliyun.com/pypi/packages/3c/77/e2362b676dc5008d81be423070dd9577fa03be5da2ba1105811900fda546/packaging-21.0-py3-none-any.whl (40 kB)
     |████████████████████████████████| 40 kB 6.1 MB/s
Collecting googleapis-common-protos<2.0dev,>=1.6.0
  Downloading https://mirrors.aliyun.com/pypi/packages/55/08/796a6bc0b550e2b7116041c953d3d5100016abea106131d71e5651826e7b/googleapis_common_protos-1.53.0-py2.py3-none-any.whl (198 kB)
     |████████████████████████████████| 198 kB 136.3 MB/s
Collecting setuptools
  Downloading https://mirrors.aliyun.com/pypi/packages/bd/25/5bdf7f1adeebd4e3fa76b2e2f045ae53ee208e40a4231ad0f0c3007e4353/setuptools-57.4.0-py3-none-any.whl (819 kB)
     |████████████████████████████████| 819 kB 127.0 MB/s
Requirement already satisfied: pytz in /usr/local/lib/python3.6/site-packages (from google-api-core<2.0.0,>=1.0.0->opencensus->ray[default]) (2020.1)
Collecting cachetools<5.0,>=2.0.0
  Downloading https://mirrors.aliyun.com/pypi/packages/bf/28/c4f5796c67ad06bb91d98d543a5e01805c1ff065e08871f78e52d2a331ad/cachetools-4.2.2-py3-none-any.whl (11 kB)
Collecting pyasn1-modules>=0.2.1
  Downloading https://mirrors.aliyun.com/pypi/packages/95/de/214830a981892a3e286c3794f41ae67a4495df1108c3da8a9f62159b9a9d/pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
     |████████████████████████████████| 155 kB 21.5 MB/s
Collecting rsa<5,>=3.1.4
  Downloading https://mirrors.aliyun.com/pypi/packages/e9/93/0c0f002031f18b53af7a6166103c02b9c0667be528944137cc954ec921b3/rsa-4.7.2-py3-none-any.whl (34 kB)
Collecting pyparsing>=2.0.2
  Downloading https://mirrors.aliyun.com/pypi/packages/8a/bb/488841f56197b13700afd5658fc279a2025a39e22449b7cf29864669b15d/pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
     |████████████████████████████████| 67 kB 7.2 MB/s
Collecting pyasn1<0.5.0,>=0.4.6
  Downloading https://mirrors.aliyun.com/pypi/packages/62/1e/a94a8d635fa3ce4cfc7f506003548d0a2447ae76fd5ca53932970fe3053f/pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
     |████████████████████████████████| 77 kB 7.0 MB/s
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/site-packages (from requests->ray[default]) (2020.6.20)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/site-packages (from requests->ray[default]) (1.25.10)
Collecting immutables>=0.9
  Downloading https://mirrors.aliyun.com/pypi/packages/4a/52/e64a14a99c509cbdfe0405e9f076aef0331cb9548a3efa1d5bacd524978a/immutables-0.15-cp36-cp36m-manylinux1_x86_64.whl (100 kB)
     |████████████████████████████████| 100 kB 18.2 MB/s
Using legacy 'setup.py install' for idna-ssl, since package 'wheel' is not installed.
Using legacy 'setup.py install' for gpustat, since package 'wheel' is not installed.
Using legacy 'setup.py install' for nvidia-ml-py3, since package 'wheel' is not installed.
Using legacy 'setup.py install' for contextvars, since package 'wheel' is not installed.
Installing collected packages: pyasn1, setuptools, rsa, pyparsing, pyasn1-modules, protobuf, multidict, immutables, cachetools, yarl, packaging, idna-ssl, googleapis-common-protos, google-auth, contextvars, attrs, async-timeout, pyrsistent, psutil, opencensus-context, nvidia-ml-py3, hiredis, google-api-core, blessings, aiohttp, redis, pyyaml, pydantic, py-spy, prometheus-client, opencensus, numpy, msgpack, jsonschema, grpcio, gpustat, filelock, colorama, aioredis, aiohttp-cors, ray, colorful
  Attempting uninstall: setuptools
    Found existing installation: setuptools 39.2.0
    Uninstalling setuptools-39.2.0:
      Successfully uninstalled setuptools-39.2.0
    Running setup.py install for idna-ssl ... done
    Running setup.py install for contextvars ... done
    Running setup.py install for nvidia-ml-py3 ... done
    Running setup.py install for gpustat ... done
Successfully installed aiohttp-3.7.4.post0 aiohttp-cors-0.7.0 aioredis-1.3.1 async-timeout-3.0.1 attrs-21.2.0 blessings-1.7 cachetools-4.2.2 colorama-0.4.4 colorful-0.5.4 contextvars-2.4 filelock-3.0.12 google-api-core-1.31.0 google-auth-1.33.1 googleapis-common-protos-1.53.0 gpustat-0.6.0 grpcio-1.39.0 hiredis-2.0.0 idna-ssl-1.1.0 immutables-0.15 jsonschema-3.2.0 msgpack-1.0.2 multidict-5.1.0 numpy-1.19.5 nvidia-ml-py3-7.352.0 opencensus-0.7.13 opencensus-context-0.1.2 packaging-21.0 prometheus-client-0.11.0 protobuf-3.17.3 psutil-5.8.0 py-spy-0.3.7 pyasn1-0.4.8 pyasn1-modules-0.2.8 pydantic-1.8.2 pyparsing-2.4.7 pyrsistent-0.18.0 pyyaml-5.4.1 ray-1.4.1 redis-3.5.3 rsa-4.7.2 setuptools-57.4.0 yarl-1.6.3

3、单机

下面是不使用分布式的代码示例:

# test_ray.py
import ray
import cv2
import numpy

import time

#@ray.remote
def test(img):
    orb = cv2.AKAZE_create()
    kb = orb.detect(img, None)
    kp, des = orb.compute(img, kp)
    return des

# 不使用注解的方式
test_remote = ray.remote(test)

# 函数test 加注解时使用方式
# test_remote = test.remote(test)

img = cv2.imread("/tmp/test.jpg")

start_time = time.time()

for i in range(10):
    test(img)

print(time.time() - start_time)

运行:python test_ray.py

4、集群

如果需要启动集群模式,则需要先启动服务,选择一台机器作为主服务器,然后按虾米那命令启动:

[root@node1 ~]# ray start --head --port=8888
Local node IP: 192.168.0.81
2021-07-28 09:58:44,051 INFO services.py:1274 -- View the Ray dashboard at http://127.0.0.1:8265

--------------------
Ray runtime started.
--------------------

Next steps
  To connect to this Ray runtime from another node, run
    ray start --address='192.168.0.81:8888' --redis-password='5241590000000000'

  Alternatively, use the following Python code:
    import ray
    ray.init(address='auto', _redis_password='5241590000000000')

  If connection fails, check your firewall settings and network configuration.

  To terminate the Ray runtime, run
    ray stop

可以看到里面提示具体的用法。此时通过ray的web界面,端口8265访问,看到已有一台机器。

然后在另外两台机启动客户端节点,保证各个节点的ray版本一致,查看版本:

# pip freeze |grep ray

版本不一致的话,要升级为一致:pip install -U ray, 此处选择的版本为1.4.1

从节点启动方式(密码可以在主节点启动时显示):

ray start --address='192.168.0.81:8888' --redis-password='5241590000000000'

改写程序,用于分布式:

# test_ray.py
import ray
import cv2
import numpy

import time

@ray.remote
def test(img):
    orb = cv2.AKAZE_create()
    kb = orb.detect(img, None)
    kp, des = orb.compute(img, kp)
    return des

# 不使用注解的方式
#test_remote = ray.remote(test)

# 函数test 加注解时使用方式
test_remote = test.remote(test)

if __name__ == "__main__":
    ray.init(address='auto', _redis_password='5241590000000000')
    start_time = time.time()
    img = cv2.imread("/tmp/test.jpg")
    futures = [test_remote.remote(img) for i in range(100)]
    ray.get(futures)
    start_time = time.time()
    print(time.time() - start_time)

在主节点运行程序,程序会在3台机器上启动。停止ray服务的方式:ray stop.

到此这篇关于支持python的分布式计算框架Ray的文章就介绍到这了,更多相关python的分布式计算框架Ray内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Python并行分布式框架Celery详解

    Celery 简介 除了redis,还可以使用另外一个神器---Celery.Celery是一个异步任务的调度工具. Celery 是 Distributed Task Queue,分布式任务队列,分布式决定了可以有多个 worker 的存在,队列表示其是异步操作,即存在一个产生任务提出需求的工头,和一群等着被分配工作的码农. 在 Python 中定义 Celery 的时候,我们要引入 Broker,中文翻译过来就是"中间人"的意思,在这里 Broker 起到一个中间人的角色.在工头提

  • python分布式计算dispy的使用详解

    dispy,是用asyncoro实现的分布式并行计算框架. 框架也是非常精简,只有4个组件,在其源码文件夹下可以找到: dispy.py (client) provides two ways of creating "clusters": JobCluster when only one instance of dispy may run and SharedJobCluster when multiple instances may run (in separate processe

  • Python利用multiprocessing实现最简单的分布式作业调度系统实例

    介绍 Python的multiprocessing模块不但支持多进程,其中managers子模块还支持把多进程分布到多台机器上.一个服务进程可以作为调度者,将任务分布到其他多个机器的多个进程中,依靠网络通信.想到这,就在想是不是可以使用此模块来实现一个简单的作业调度系统.在这之前,我们先来详细了解下python中的多进程管理包multiprocessing. multiprocessing.Process multiprocessing包是Python中的多进程管理包.它与 threading.

  • Python搭建Spark分布式集群环境

    前言 Apache Spark 是一个新兴的大数据处理通用引擎,提供了分布式的内存抽象.Spark 最大的特点就是快,可比 Hadoop MapReduce 的处理速度快 100 倍.本文没有使用一台电脑上构建多个虚拟机的方法来模拟集群,而是使用三台电脑来搭建一个小型分布式集群环境安装. 本教程采用Spark2.0以上版本(比如Spark2.0.2.Spark2.1.0等)搭建集群,同样适用于搭建Spark1.6.2集群. 安装Hadoop并搭建好Hadoop集群环境 Spark分布式集群的安装

  • python django框架中使用FastDFS分布式文件系统的安装方法

    一.安装FastDFS 1-1:执行docker命令安装 # 安装tracker docker run -dti --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs youkou1/fastdfs tracker # 安装storage docker run -dti --network=host --name storage -e TRACKER_SERVER=IP地址:22122 -v /var/fdfs/storage:

  • Python实现的redis分布式锁功能示例

    本文实例讲述了Python实现的redis分布式锁功能.分享给大家供大家参考,具体如下: #!/usr/bin/env python # coding=utf-8 import time import redis class RedisLock(object): def __init__(self, key): self.rdcon = redis.Redis(host='', port=6379, password="", db=1) self._lock = 0 self.lock

  • Python使用multiprocessing实现一个最简单的分布式作业调度系统

    mutilprocess像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多. 介绍 Python的multiprocessing模块不但支持多进程,其中managers子模块还支持把多进程分布到多台机器上.一个服务进程可以作为调度者,将任务分布到其他多个机器的多个进程中,依靠网络通信. 想到这,就在想是不是可以使用此模块来实现一个简单的作业调度系统. 实现 Job 首先创建一个Job类,为了测试简单,只包含一

  • 支持python的分布式计算框架Ray详解

    项目地址:https://github.com/ray-project/ray 1.简介 Ray为构建分布式应用程序提供了一个简单.通用的API.Ray是一种分布式执行框架,便于大规模应用程序和利用先进的机器学习库. Ray通过以下方式完成这项任务: 为构建和运行分布式应用程序提供简单的原语. 使最终用户能够并行化单个机器代码,而代码更改很少到零. 在核心Ray之上包含大量应用程序.库和工具,以支持复杂的应用程序. 2.安装 安装方式比较简单: pip install ray==1.4.1 [r

  • python中unittest框架应用详解

    目录 1.Unittest为Python内嵌的测试框架,不需要特殊配置 2.编写规范 总结 1.Unittest为Python内嵌的测试框架,不需要特殊配置 2.编写规范 需要导入 import unittest 测试类必须继承unittest.TestCase 测试方法以 test_开头 模块和类名没有要求 TestCase 理解为写测试用例 TestSuite 理解为测试用例的集合 TestLoader 理解为的测试用例加载 TestRunner 执行测试用例,并输出报告 import un

  • python+appium+yaml移动端自动化测试框架实现详解

    结构介绍 之前分享过一篇安卓UI测试,但是没有实现数据与代码分离,后期维护成本较高,所以最近抽空优化了一下. 不想看文章得可以直接去Github,欢迎拍砖 大致结构如下: testyaml管理用例,实现数据与代码分离,一个模块一个文件夹 public 存放公共文件,如读取配置文件.启动appium服务.读取Yaml文件.定义日志格式等 page 存放最小测试用例集,一个模块一个文件夹 results 存放测试报告及失败截图 logs 存放日志 testcase 存放测试用例runtest.py

  • python中的unittest框架实例详解

    在python中我们学习了不少理论知识,那么对相关的程序进行测试,就显得很重要了.本篇要讲的是unittest框架,我们可以用它来做一些测试工作,又或者是相关代码的编写.下面我们就unittest框架的说明.特性和4种字模块分别带来介绍,大家一起来看具体内容. 1.unittest说明 unittest是Python自带的单元测试框,具备编写用例.组织用例.执行用例.输出报告等自动化框架的条件,可以用来作自动化测试框架的用例组织执行框架. 2.unittest框架特性 (1)提供用例组织与执行:

  • Python实现简易Web爬虫详解

    简介: 网络爬虫(又被称为网页蜘蛛),网络机器人,是一种按照一定的规则,自动地抓信息的程序或者脚本.假设互联网是一张很大的蜘蛛网,每个页面之间都通过超链接这根线相互连接,那么我们的爬虫小程序就能够通过这些线不断的搜寻到新的网页. Python作为一种代表简单主义思想的解释型.面向对象.功能强大的高级编程语言.它语法简洁并且具有动态数据类型和高层次的抽象数据结构,这使得它具有良好的跨平台特性,特别适用于爬虫等程序的实现,此外Python还提供了例如Spyder这样的爬虫框架,BeautifulSo

  • Python中的asyncio代码详解

    asyncio介绍 熟悉c#的同学可能知道,在c#中可以很方便的使用 async 和 await 来实现异步编程,那么在python中应该怎么做呢,其实python也支持异步编程,一般使用 asyncio 这个库,下面介绍下什么是 asyncio : asyncio 是用来编写 并发 代码的库,使用 async/await 语法. asyncio 被用作多个提供高性能 Python 异步框架的基础,包括网络和网站服务,数据库连接库,分布式任务队列等等. asyncio 往往是构建 IO 密集型和

  • 对python多线程与global变量详解

    今天早上起来写爬虫,基本框架已经搭好,添加多线程爬取功能时,发现出错: 比如在下载文件的url列表中加入200个url,开启50个线程.我的爬虫-竟然将50个url爬取并全部命名为0.html,也就是说,最后的下载结果,是有1个0.html(重复的覆盖了),还有1-150.下面是我的代码: x = str(theguardian_globle.g) #x为给下载的文件命的名 filePath = "E://wgetWeiBao//"+x+".html" try: w

  • Python API 操作Hadoop hdfs详解

    http://pyhdfs.readthedocs.io/en/latest/ 1:安装 由于是windows环境(linux其实也一样),只要有pip或者setup_install安装起来都是很方便的 >pip install hdfs 2:Client--创建集群连接 > from hdfs import * > client = Client("http://s100:50070") 其他参数说明: classhdfs.client.Client(url, ro

  •  python用matplotlib可视化绘图详解

    目录 1.Matplotlib 简介 2.Matplotlib图形绘制 1)折线图 2)柱状图 3)条形图 3)饼图 4)散点图 5)直方图 6)箱型图 7)子图 1.Matplotlib 简介 Matplotlib 简介: Matplotlib 是一个python的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形,matplotlib 对于图像美化方面比较完善,可以自定义线条的颜色和样式,可以在一张绘图纸上绘制多张小图,也可以在一张图上绘制多条线,可以很方便地将数据可

随机推荐