berlindb / core

All of the required core code
MIT License
253 stars 27 forks source link

Discussion - Asynchronous Actions #58

Closed alexstandiford closed 4 years ago

alexstandiford commented 4 years ago

I really wish it were possible to do asynchronous actions in BerlinDB. There are numerous instances in-which a process does not need to wait for a database change to happen to continue.

A simple example is deleting related data from other related tables, such as metadata.

There are also less-common scenarios where the system needs to save data, but does not need to use that record right away.

I recognize that PHP doesn't have any true async capabilities like Javascript, but perhaps we could set something up that runs a set of operations in a separate request.

I'm writing this with the intent to discuss different ways to approach this, identify pitfalls, and even identify if this sort of thing should be in BerlinDB at all.

ashleyfae commented 4 years ago

My first thought is that it's out of scope for BerlinDB and is better implemented separately, such as via integrating with wp-background-processing or Gearman etc. Click a button, that fires off a background task, and the callback of that task uses BerlinDB. But the actual part that makes it asynchronous isn't BerlinDB itself.

But if you feel otherwise then I'd love to hear the reasoning.

alexstandiford commented 4 years ago

Well, I suppose the thought that crossed my mind was that there could be another argument on query actions (delete, add_item) that could basically say “do this action asynchronously”. It would gather up all the async tasks, and do them in a separate thread, perhaps it’s fired on php shutdown.

I don’t think it would be intended to be a be-all-end-all solution to async, not at all. I do think it could provide a down-and-dirty, accessible way to make Berlin run faster on some implementations.

I’m definitely on the fence, but I also lean toward “out of scope”. I’m pretty inexperienced with async php, so I don’t fully understand the nuance well enough to get the implications I’m making.

alexstandiford commented 4 years ago

I figured it could work via wp_remote_post with async set to true. A button in an interface, and using Jacascript to do this makes sense but it’s not always possible to do it that way - sometimes I want the data to be adapted in the background during a typical request.

JJJ commented 4 years ago

WordPress doesn't make it easy to implement asynchronicity, due to everything being completely dependent on the HTTP request.

There are a few different ways of doing async in PHP these days, but I wouldn't call it popular or easy. This will likely require a lot of work.

Berlin will theoretically be more easily adapted into an asynchronous framework once #39 happens.

Tend to agree that it's somewhat out of scope for core Berlin, but all of the core code should at least be async-friendly as we go!

alexstandiford commented 4 years ago

Yep, okay I'm in on that. Even if we build something in, it may prevent other async systems from working properly.

Okay, I'm going to go ahead and close this.