今天,我找到了一个名为trio的库,它表示自己是一个针对人类的异步API.这些词与requests' 有点类似.由于这requests是一个很好的图书馆,我想知道它的优点是什么trio.
关于它的文章不多,我只是找到一篇文章讨论curio和asyncio.令我惊讶的是,trio它本身甚至比curio(下一代古玩)更好.
阅读完一半文章后,我找不到这两个异步框架之间的核心区别.它只是给出了一些实例curio比实现更方便的例子asyncio.但底层结构几乎相同(基于回调,我认为所有异步IO框架都基于回调而没有任何异常.)
那么有人能给我一个理由我必须接受trio或者curio更好asyncio吗?或者解释一下为什么我应该选择trio而不是内置asyncio?
当我启动docker quickstart终端时,它被"等待IP"阻塞,根本没有响应.
有人可以告诉我如何处理?
我无法弄清楚的是,尽管ThreadPoolExecutor使用守护进程工作者,即使主线程退出,它们仍然会运行.
我可以在python3.6.4中提供一个最小的例子:
import concurrent.futures
import time
def fn():
while True:
time.sleep(5)
print("Hello")
thread_pool = concurrent.futures.ThreadPoolExecutor()
thread_pool.submit(fn)
while True:
time.sleep(1)
print("Wow")
Run Code Online (Sandbox Code Playgroud)
主线程和工作线程都是无限循环.所以如果我KeyboardInterrupt用来终止主线程,我希望整个程序也会终止.但实际上工作线程仍在运行,即使它是一个守护程序线程.
ThreadPoolExecutor确认工作线程是守护程序线程的源代码:
t = threading.Thread(target=_worker,
args=(weakref.ref(self, weakref_cb),
self._work_queue))
t.daemon = True
t.start()
self._threads.add(t)
Run Code Online (Sandbox Code Playgroud)
此外,如果我手动创建一个守护程序线程,它就像一个魅力:
from threading import Thread
import time
def fn():
while True:
time.sleep(5)
print("Hello")
thread = Thread(target=fn)
thread.daemon = True
thread.start()
while True:
time.sleep(1)
print("Wow")
Run Code Online (Sandbox Code Playgroud)
所以我真的无法弄清楚这种奇怪的行为.
我的意思是我想指定多个分支,例如dev|master。但是看完文档后,我认为这是不可能的??我是否仅为了使用同一规则来保护两个分支就必须创建两个规则?
我正试图tqdm通过多个过程来使用.而且行为并不像预期的那样.我认为重点是价值pbar不会通过流程更新.那么如何处理这个问题呢?我也尝试过手动Value更新pbar.n,但仍然失败了.它似乎tqdm不支持更新值并手动渲染.
def test(lock, pbar):
for i in range(10000):
sleep(0.1)
lock.acquire()
pbar.update()
lock.release()
pbar = tqdm(total = 10000)
lock = Lock()
for i in range(5):
Process(target = test, args = (lock, pbar))
Run Code Online (Sandbox Code Playgroud) 等待时如何继续下一个循环?例如:
async def get_message():
# async get message from queue
return message
async process_message(message):
# make some changes on message
return message
async def deal_with_message(message):
# async update some network resource with given message
async def main():
while True:
message = await get_message()
message = await process_message(message)
await deal_with_message(message)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)
如何使while True循环并发?如果它正在等待deal_with_message,它可以进入下一个循环并运行get_message?
我想我已经找到了解决方案:
async def main():
asyncio.ensure_future(main())
message = await get_message()
message = await process_message(message)
await deal_with_message(message)
loop = asyncio.get_event_loop() …Run Code Online (Sandbox Code Playgroud) 当我在 PyCharm 社区版中编写 Python3 代码时,我对导入逻辑感到恼火,下面是我的结构。
\n\nproject\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 orm.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 coroweb.py\nRun Code Online (Sandbox Code Playgroud)\n\n在 app.py 中,我使用下面的代码导入 orm 。
\n\nfrom . import orm\nfrom .coroweb import xxx\nRun Code Online (Sandbox Code Playgroud)\n\n它在 PyCharm 中看起来不错,但是当我运行 app.py 时,它会由于以下错误而失败
\n\nImportError: cannot import name 'orm'\nRun Code Online (Sandbox Code Playgroud)\n\n然后,我像这样使用相关导入。
\n\nimport orm\nfrom coroweb import xxx\nRun Code Online (Sandbox Code Playgroud)\n\n它运行良好,但在 PyCharm 中出现导入错误。它是Unresolved reference。我想在 PyCharm 中使用代码跟踪,所以我想知道如何解决这个问题。
我正在运行一个容器,我只想允许它访问特定的 ip。换句话说,我想拒绝大多数目标ip。
我尝试了以下方法:
iptables -I DOCKER-USER -o custom-interface ! -d xxx.xxx.xxx.xxx -j REJECT
Run Code Online (Sandbox Code Playgroud)
但它拒绝所有连接,我不能ping xxx.xxx.xxx.xxx。
真的很奇怪,我想我只是通过不会到达xxx.xxx.xxx.xxx的自定义接口阻止输出数据包。因此,所有到达 xxx.xxx.xxx.xxx 的传入数据包和输出数据包都被接受。
但似乎我错了。为什么?任何帮助表示赞赏。
接受的答案显示了如何配置传入限制,然后我学习了如何配置传出限制。
iptables -N BEFORE_DOCKER
Run Code Online (Sandbox Code Playgroud)
iptables -I BEFORE_DOCKER -j DROP
Run Code Online (Sandbox Code Playgroud)
iptables -I BEFORE_DOCKER -o eth0 -d 172.114.1.23 -j ACCEPT
iptables -I BEFORE_DOCKER -o eth0 -d 10.129.172.12 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)
iptables -I BEFORE_DOCKER -o eth1 -d 192.168.10.1 -j ACCEPT
iptables -I BEFORE_DOCKER …Run Code Online (Sandbox Code Playgroud) 一切都是文件搜索程序。由于其作者尚未发布源代码,我想知道它是如何工作的。
引用其常见问题解答,
“Everything” 只索引文件和文件夹名称,通常需要几秒钟来构建它的数据库。全新安装的 Windows 10(大约 120,000 个文件)将需要大约 1 秒的时间来编制索引。1,000,000 个文件大约需要 1 分钟。
如果索引整个Windows 10只需要一秒钟,而索引一百万个文件只需要1分钟,这是否意味着它每秒可以索引120,000个文件?
为了使搜索快速,必须有一个特殊的数据结构。按文件名搜索不仅从头搜索,大多数情况下也从中间搜索。这使得一些广泛使用的索引结构如Trie和Red–black tree无效。
常见问题进一步澄清。
“一切”会占用我的系统资源吗?
不,“一切”使用很少的系统资源。全新安装的 Windows 10(大约 120,000 个文件)将使用大约 14 MB 的内存和不到 9 MB 的磁盘空间。1,000,000 个文件将使用大约 75 MB 的内存和 45 MB 的磁盘空间。
Ubuntu 18.x + selenium webdriver(Firefox)
面对一个奇怪的问题,如果我run将它们全部放在一起,下面的块就可以工作了
from selenium import webdriver
url = 'https://indiamart.com'
driver = webdriver.Firefox()
driver.get(url)
driver.find_element_by_xpath(xpath).click()
Run Code Online (Sandbox Code Playgroud)
每次url尝试过都会发生这种情况.
但是,如果我一次执行一行,它会给出
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/media/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 326, in get
self.execute(Command.GET, {'url': url})
File "/media/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
response = self.command_executor.execute(driver_command, params)
File "/media/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 472, in execute
return self._request(command_info[0], url, body=data)
File "/media/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 495, in _request
self._conn.request(method, parsed_url.path, body, headers)
File "/usr/lib/python3.6/http/client.py", line 1239, in request …Run Code Online (Sandbox Code Playgroud) 我收到以下错误:
from azure.storage.blob import BlockBlobService
ImportError: cannot import name 'BlockBlobService'
Run Code Online (Sandbox Code Playgroud)
尝试使用命令提示符运行我的 python 项目时。(当我直接从 anaconda navigator 执行代码时,它似乎可以工作。)
我正在为 Anaconda 使用 Python 3.6.4。跑步pip freeze给了我以下内容:
azure-nspkg==2.0.0
azure-storage-blob==1.1.0
azure-storage-common==1.1.0
azure-storage-nspkg==3.0.0
azurepython3==1.7.7
Run Code Online (Sandbox Code Playgroud) 我已经完成了以下测试,我发现我无法psutil用来检索有意义的数据。
基本上我试图运行这个命令来使用 python 报告容器内的可用内存。
我已经运行了 3 次测试,每次都使用不同的内存参数:
以下是测试:
docker run -m 10000000 -ti python:2.7 bash
docker run -m 100000000 -ti python:2.7 bash
docker run -m 800000000 -ti python:2.7 bash
Run Code Online (Sandbox Code Playgroud)
这是结果
> docker run -m 10000000 -ti python:2.7 bash
root@310de7b416cc:/# pip install psutil && python -c "import psutil; print psutil.virtual_memory().available"
Killed
> docker run -m 100000000 -ti python:2.7 bash
root@e7aade23c143:/# pip install psutil && python -c "import psutil; print psutil.virtual_memory().available"
Collecting psutil
Downloading psutil-5.3.1.tar.gz (397kB)
100% |????????????????????????????????| 399kB 2.8MB/s …Run Code Online (Sandbox Code Playgroud) 我不熟悉打包前端项目。当我在写前端时,我们只使用了 JQuery。所以问题是现在我有一个由 vue-cli 创建并由 webpack 打包的项目。
但是因为我不想从本地服务器加载库,而是从远程 CDN 加载库。我应该如何将yarn add依赖项更改为基于 CDN 的形式yarn build?进行这种包装的正确方法是什么?
我搜索了很多但找不到好的解决方案,有些人可能会建议在 head 部分添加所有 CDN。但这很难管理。
python ×8
docker ×3
python-3.x ×3
asynchronous ×1
cdn ×1
concurrency ×1
curio ×1
daemon ×1
event-loop ×1
file-search ×1
frontend ×1
github ×1
iptables ×1
linux ×1
networking ×1
performance ×1
pycharm ×1
python-2.7 ×1
python-trio ×1
search ×1
selenium ×1
tqdm ×1
ubuntu ×1
vue.js ×1
webpack ×1