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

my code which i written it throws me an Invalid syntax error at try:

我试过在没有 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