How come my Python code doesn't work?
from celery.decorators import task
from celery.decorators import task
@task()
def add(x, y):
r = open("./abc.txt","w")
r.write("sdf")
r.close()
return x + y
That's my tasks.py file.
开发者_如何学运维>>> import tasks
>>> r = tasks.add.delay(3,5)
>>> r.result
8
As you can see, the function works. However, the file does not create. Why?
I've tried changing multiple file paths, due to possible permission issues. but no luck.
If the file was not being written, you would get an exception, so the function cannot possibly complete.
Since the function is returning 8, it follows that the file is being written somewhere.
Perhaps the file is written in a different directory to the one you are expecting
The only other possibility I can think of is that the add function that is being run is not the one that you have shown here
I think the problem is that you're running this by importing a module. The .
in the file path is relative to where the module lives, not your current working directory. Try giving it a full path name.
If that doesn't work, show us exactly where you're running the script from and an ls -la
on that directory. And if that still doesn't show anything abnormal. Do a
find / -name abc.txt
- The working directory may not be the one you expect.
- The task may be done on another host, if you use multiple hosts.
精彩评论