python - 我编写的代码在尝试 : 时抛出无效语法错误

标签 python ray

我尝试在没有 def 的情况下做到这一点,并且有效,这是我真的不知道该怎么做的代码,我也尝试正确给出缩进

但是当我运行它时,它会抛出错误无效语法

import ray

ray.init()

@ray.remote
try:
    Func1()
except:
    pass



ray.get([func1.remote()])



def func1():
    for i in range (99999):
        print("h")```

The error is invalid syntax at try:




This is the code that worked for me 


try:
    do_something()
except:
    pass



最佳答案

您不能在 try/except 之上使用像 @ray.remote 这样的装饰器,这会导致 SyntaxError

您需要将 try/except 包装在函数周围,并装饰该函数。例如

@ray.remote
def Func2():
    try:
        Func1()
    except:
        pass

关于python - 我编写的代码在尝试 : 时抛出无效语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56319476/

相关文章:

python - 无法使用python将JSON文件从谷歌云存储加载到bigquery

python-3.x - 什么是 ray::IDLE 以及为什么有些 worker 内存不足?

python - 射线 `num_cpus` 是否用于实际分配 CPU?

python-3.x - 射线标准输出去了哪里?

python-3.x - 如何在 Ray Tune 中定义与搜索算法无关的高维搜索空间?

tensorflow - 访问优化器内部状态

python - 从 Python 解释器运行时获取 "ImportError: attempted relative import with no known parent package"

python - 当值为 mongodb 中其他对象的 _id 时,无法使用 python-eve 过滤字符串字段

java - 使用 Jython 和 Swing 处理事件

python - Sphinx autodoc 给出警告 : py:class reference target not found: type warning