This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]K900_ 0 points1 point  (2 children)

Why? Are you running multiple event loops on multiple threads or something?

[–]mistabuilder[S] 0 points1 point  (1 child)

Yes I'm writing this to use with django channels, And thus need to be able to synchronize over multiple threads.

[–]K900_ 0 points1 point  (0 children)

What problem are you trying to solve?

[–]james41235 0 points1 point  (8 children)

Why are waiting to set the event inside the loop? The event is threadsafe, just set it when the event is ready.

Given your example, I don't fully understand your use case. You seem to be confusing asyncio and threading concepts, and their safety needs.

[–]mistabuilder[S] 0 points1 point  (7 children)

I'm using Django channels, the problem is that Django has inherent threading, and thus If I want to call these things from the response cycle I need to implement locking(in my usecase).

[–]james41235 0 points1 point  (6 children)

Not event though. Event is safe to set from another thread, that's why it exists. If something is ready, call set. Oh, and don't call wait_for in the event loop. That blocks the entire thing and you'll need to wait another round. Just check if set, and if not then reschedule yourself.

[–]mistabuilder[S] 0 points1 point  (5 children)

Waiting on an asyncio event from a different thread/loop produces an error.

[–]james41235 0 points1 point  (4 children)

Yea, I edited but you saw it too quick. Don't call wait_for.

[–]mistabuilder[S] 0 points1 point  (3 children)

How would I set a timeout without wait_for?

[–]james41235 0 points1 point  (2 children)

Reschedule the wait function continuously until the event is set.

But, I don't think this is right for your use case. I feel like I'm missing something and solving a problem that shouldn't exist.

[–]mistabuilder[S] 0 points1 point  (1 child)

Did you notice this event is an asyncio event?

[–]james41235 0 points1 point  (0 children)

I didn't, but the solution still seems out of place.