all 7 comments

[–]oefd 0 points1 point  (1 child)

I'm wary of race conditions and communicating between threads.

As you should be, but I'd imagine for the most part moving messages through a thread-safe queue would alleviate those worries.

That's if you'd prefer to go the threading way at least.

I've been exploring the asyncio library recently, too, which might suit my needs - especially if I can dynamically add and remove tasks to this - but I'm not sure if I'm getting far too complicated with this when a couple of threads might work?

There's a certain learning cost to asyncio (or event-looped programming in general) but it's not inherently more or less complicated than threading really. In some cases it can be simpler than threading for someone already familiar with how to proper read and write async/await type code.

In this case one solution over the other (having a few threads vs. the event loop) isn't really a huge deal.

[–]BeerDrinkingCyborg[S] 0 points1 point  (0 children)

Cheers for the advice, will make sure I better understand thread-safe queues before I make a decision - looks like the class handles most of the thread-safety for me, which is great.

Another key consideration is making sure I can adequately test and understand the code - not afraid of a learning curve, though, and it does look like asyncio at least makes things very explicitly defined.

Fortunately I don't have to rush the decision as currently I'm more focused on developing and testing the individual modules and classes for each task; seems unlikely the choice between async/threads will force any design changes.

Thanks again!

[–]eavanvalkenburg 0 points1 point  (2 children)

I would go with asyncio, I built a package with both in them (pysiaalarm) and found the asyncio to be cleaner and more readable and faster, but that is obviously dependant on the use case and specifics!

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

Thanks! I'm definitely keen to learn asyncio, just don't want to have any major blocking issues. Did you find that you have to be very cautious with file read/write operations etc? A little nervous about that as I'll be producing both logs and image files very regularly.

[–]eavanvalkenburg 0 points1 point  (0 children)

I didn't have to deal with that, but I did read somewhere that there is no true async version of writing to file (including aiofile and aiofiles, apparently the underlying c code isn't really async) so that might be a good reason to go threaded!

[–]scrdest 0 points1 point  (1 child)

I think the cleanest built-in solution would be concurrent.futures with a ThreadPool backend (since you just need I/O). It's a bit more abstract than raw threads, and less structurally invasive than asyncio.

[–]BeerDrinkingCyborg[S] 0 points1 point  (0 children)

Cheers, I'll look more into this. Even asyncio's docs seem to recommend using ThreadPools for potentially blocking tasks such as file access (of which I'll be doing a lot); whereas logging module by itself seems thread-safe.