我如何在 numba @jit

问题描述 投票:0回答:1

我的代码:

代码尝试向google发送请求

from numba import jit
import requests
@jit()
def x():
   requests.get("https://google.com")
x()

错误:

这是我收到的错误文本

[Running] python -u "/home/nfrxd/Documents/kk.py"
Traceback (most recent call last):
  File "/home/nfrxd/Documents/kk.py", line 6, in <module>
    x()
  File "/home/nfrxd/.local/lib/python3.11/site-packages/numba/core/dispatcher.py", line 468, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/home/nfrxd/.local/lib/python3.11/site-packages/numba/core/dispatcher.py", line 409, in error_rewrite
    raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
[1m[1mUnknown attribute 'get' of type Module(<module 'requests' from '/home/nfrxd/.local/lib/python3.11/site-packages/requests/__init__.py'>)
[1m
File "kk.py", line 5:[0m
[1mdef x():
[1m   requests.get("https://google.com")
[0m   [1m^[0m[0m
[0m
[0m[1mDuring: typing of get attribute at /home/nfrxd/Documents/kk.py (5)[0m
[1m
File "kk.py", line 5:[0m
[1mdef x():
[1m   requests.get("https://google.com")
[0m   [1m^[0m[0m


[Done] exited with code=1 in 0.631 seconds

我尝试使用 njit 那么有什么方法可以修复吗?

python numba
1个回答
0
投票

我假设您了解 旨在与 NumPy 数组和函数一起使用(在其主页上有说明)。

Numba 不会 使您向 google.com 的请求更快,它主要用于加快数字处理速度。

但是如果您的示例只是代码的一部分(即在 jitted 函数中使用

requests
),您可以使用
numba.objmode
,例如:

import requests
from numba import jit, objmode


@jit()
def x():
    with objmode(t="unicode_type"):
        t = requests.get("https://google.com").text
    print(t[:15])


x()

打印:

<!doctype html>
© www.soinside.com 2019 - 2024. All rights reserved.