Sanic: How to lock thread when worker > 1

Created on 7 Jul 2017  Â·  5Comments  Â·  Source: sanic-org/sanic

I have 4 workers when sanic runs like below:

app.run(host='0.0.0.0', port=port, workers=4)

I have tried to use both multiprocessing.Lock or threading.lock to lock thread but I will get differnt lock so I can hardly lock the the process.

Is there anyway to fix this?Thank you

Most helpful comment

Why do you wanna lock thread ? This is async... web server. it's single thread usually.

All 5 comments

Why do you wanna lock thread ? This is async... web server. it's single thread usually.

@yunstanford Without thread lock I can only use write lock in database as I need consistency of data sometimes. However the database is not only used by this app so it's not a good idea to use eXclusive lock. So I wonder is there any elegent way to fix it. Thank you.

@yunstanford I believe your problem is the architecture. Try to use a job
queue to connect to the database, like celery.

Em dom, 9 de jul de 2017 às 23:47, guobuping notifications@github.com
escreveu:

@yunstanford https://github.com/yunstanford Without thread lock I can
only use write lock in database as I need consistency of data sometimes.
However the database is not only used by this app so it's not a good idea
to use eXclusive lock. So I wonder is there any elegent way to fix it.
Thank you.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/channelcat/sanic/issues/838#issuecomment-313988788,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAvofKRtqlus8_vdfNm5AlCNk-B0LKzCks5sMZDVgaJpZM4OQwLO
.

@thiagoribeiro I see I can use a queue to achieve that. However it will increase the complexity. But if I can hardly find a way to fix it ,I'll try to use a queue. Thank you

@guobuping is there still an issue here or are you satisfied? Personally I think DB consistency should be handled by the DB 🤔 but I don't know the specifics of your situation.

Was this page helpful?
0 / 5 - 0 ratings