Closed YihuiLu closed 3 years ago
@Zxilly
working on this
I noticed you use a package named casbin_redis_watcher
, but I didn't find this on pypi. Could you please provide the link of that? This will be helpful to locate the problem.
Seems the problem caused by
File "/Users/luyifei/PycharmProjects/pinstreet_open_service_backend/.venv/lib/python3.7/site-packages/casbin_redis_watcher/watcher.py", line 44, in __init__
self.create_subscriber_process(start_process)
Maybe you should setup watcher and enforcer in the __main__
I guess it use popen to operate redis, which may cause unexpected error when it works with fastapi
我注意到您使用了名为的程序包
casbin_redis_watcher
,但是我在pypi上没有找到它。您能提供它的链接吗?这将有助于定位问题。似乎是由问题引起的
File "/Users/luyifei/PycharmProjects/pinstreet_open_service_backend/.venv/lib/python3.7/site-packages/casbin_redis_watcher/watcher.py", line 44, in __init__ self.create_subscriber_process(start_process)
也许您应该在
__main__
casbin_redis_watcher's home page is https://pypi.org/project/pycasbin-redis-watcher/ hope it's
casbin_redis_watcher is an officially recommended plug-in for casbin to synchronize permission descriptions for different nodes This is necessary for me
@YihuiLu As described in redis-watcher readme
This redis-watcher module starts separate processes which subscribe to a redis channel, and listens for updates to the casbin policy on that channel. When running within WSGI contexts (like uwsgi) you may want to start these processes as a postfork action.
So, you should use startup action for fastapi. ref to: https://fastapi.tiangolo.com/advanced/events/
@YihuiLu As described in redis-watcher readme
This redis-watcher module starts separate processes which subscribe to a redis channel, and listens for updates to the casbin policy on that channel. When running within WSGI contexts (like uwsgi) you may want to start these processes as a postfork action.
So, you should use startup action for fastapi. ref to: https://fastapi.tiangolo.com/advanced/events/
Thank you for your reply, I've tried to use casbin_redis_watcher in startup, but there's another problem: https://github.com/ScienceLogic/pycasbin-redis-watcher/issues/2 I know you shouldn't be asked questions that have nothing to do with fastapi-authz at this time, but you'll need these two frameworks to work with when using casbin in fastapi, so I'm looking forward to getting your help. How do I design a code structure to run successfully?
Try something like
proc = None
def run():
uvicorn.run(app=app, host=host, port=port)
def start():
global proc
proc = Process(target=run, args=(), daemon=True)
proc.start()
and use start() in the __main__
In fact, this is still not a good idea. pycasbin-redis-watcher looks not designed for asyncio. Maybe later I will create a asyncio version.
In fact, this is still not a idea. pycasbin-redis-watcher looks not designed for asyncio. Maybe later I will create a asyncio version.
I look forward to your writing of an asyncio version, and I hope to be able to help you.
For the time being, I might consider giving up Casbin and Fastapi for the time being, because it's more difficult to
But if I can set a plan for writing an asyncio version of Watcher, I'm willing to participate and make it part of my job
I throw an error when I use the following code