重新使用ThreadPoolExecutor的后台任务的工作进程装饰器。

soon的Python项目详细描述


#很快
后台任务的worker decorator将重新使用[threadpoolexecutor](https://docs.python.org/3/library/concurrent.futures.html concurrent.futures.threadpoolexecutor)。

` setup.py`
```
git clone git@github.com:dotpot/soon.git
sudo python setup.py install
````


要确保很快正确安装,可以从项目根目录运行unittest套件:
``````
pipenv run pytest-v
````

用法
----
soon库使您能够利用多线程的好处最不关心实现细节。



website fetcher example
----
您已经收集了一个url列表,并希望下载该部分的html。以下是解决任务的第一步,这是非常合理的。

`` python
url=[
'https://cnn.com',
'https://news.ycombinator.com/',
'https://stackoverflow.com/',


`````````````python
import time
import-requests


defetch(url):
return requests.get(url)








start=time.time()
respons=[fetch(url)fetch(url)fetch(url)fetch(url)url=
html=[respons.response.text响应]
end=time.time.time()
br/>print(“时间:%秒”%(结束-开始))
```
--


>更有效的网站获取程序示例
----


>使用soon的decorator语法,我们可以定义在多线程中执行的函数。对“download”的单独调用是非阻塞的,但我们可以忽略这一事实,并按照同步范例中的方式编写代码。

``` python
导入时间
来自即将导入的工作线程的导入请求


@workers(5)
def fetch(url):
返回请求。get(url)


如果u name_uuu==”uu main_u”:
开始=时间。time()
响应=[url中url的获取(url)
html=[响应中的响应文本]
结束=time.time()
print(“time:f seconds”%(end-start))

````
我们现在可以更有效地下载网站。

--


防止挂起无法保证返回的任务。

``python
import time


@soon import workers

@workers(1,timeout=0.1)
def timeout_error():
time.sleep(1)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在依赖项上没有这样的方法异常   java如何将@Value注入setter?   验证输入java的最佳实践   java在使用kotlin的安卓项目中没有此类字段错误   我们可以强制java接口实现超时吗?   java将BasicAuth()与FormLogin()组合用于不同的路径   java无法在更新SDK后更新应用程序(未安装应用程序)   macos Java 8、Swing和OSX:对话框使UI对鼠标无响应   java自定义单元格格式以US格式显示,尽管使用了带UK语言环境的DataFormatter   java按升序排列列表字符串数据   java Hibernate OGM[PersistenceUnit:person]无法构建Hibernate SessionFactory   生命周期配置未涵盖java插件执行:com。乱穿马路。专家插件。安卓generation2:安卓mavenplugin:3.5.0:generatesources   java RecyclerView显示为空   当服务器不返回任何输入时,java InputStream read()会阻塞