python单机并发_Python中的并发控制

最近有个需求,要大批量写数据,项目是用Python写的。所以有个问题就是如何使用Python做并发控制,如果是Go语言,那么就可以使用 WaitGroup:

package main

import (

"sync"

)

type httpPkg struct{}

func (httpPkg) Get(url string) {}

var http httpPkg

func main() {

var wg sync.WaitGroup

var urls = []string{

"http://www.golang.org/",

"http://www.google.com/",

"http://www.somestupidname.com/",

}

for _, url := range urls {

// Increment the WaitGroup counter.

wg.Add(1)

// Launch a goroutine to fetch the URL.

go func(url string) {

// Decrement the counter when the goroutine completes.

defer wg.Done()

// Fetch the URL.

http.Get(url)

}(url)

}

// Wait for all HTTP fetches to complete.

wg.Wait()

}

但是这样还只能做到等待所有任务完成后再退出,但是并发控制就得用别的方法,比如使用一个channel或者是加锁。但是Python总是有 非常成熟的方案:concurrent.futures.Executor。

Executor有两个具体实现:ThreadPoolExecutor 和 ProcessPoolExecutor,分别是线程池和进程池。用法都是一样的,都符合 Executor 的接口定义。我们来看实际例子:

from concurrent.futures import ThreadPoolExecutor

import time

def loooooong_task(i):

print("task %s sleeping..." % i)

time.sleep(10)

print("task %s done..." % i)

with ThreadPoolExecutor(max_workers=2) as executor:

for i in range(10):

executor.submit(loooooong_task, i)

是不是非常简单,这就是Python的魔力所在。

附:另外还可以参考参考 gevent的pool

参考资料:

更多文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值