1

This is more of a theorical question.

Well, imagine that I have two programas that work simultaneously, the main one only do something when he receives a flag marked with true from a secondary program. So, this main program has a function that will keep asking to the secondary for the value of the flag, and when it gets true, it will do something.

What I learned at college is that the polling is the simplest way of doing that. But when I started working as an developer, coworkers told me that this method generate some overhead or it's waste of computation, by asking every certain amount of time for a value.

I tried to come up with some ideas for doing this in a different way, searched on the internet for something like this, but didn't found a useful way about how to do this.

I read about interruptions and passive ways that can cause the main program to get that data only if was informed by the secondary program. But how this happen? The main program will need a function to check for interruption right? So it will not end the same way as before?

What could I do differently?

example

Pedro Vieira
  • 2,266
  • 1
  • 20
  • 35
  • You're describing "polling", have a read of https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern and further design patterns. Depending on the communication technology between the two systems you will implement these differently. – StuperUser Aug 01 '18 at 14:25
  • Thanks, I will read the link to see if I can find the answer, and edit the question to add the name of the method. – Pedro Vieira Aug 01 '18 at 14:30
  • `see if I can find the answer` if you're looking for a specific answer to a general problem, this question will likely get closed as being too broad since there are a number of different ways to eradicate polling between two entities. – StuperUser Aug 01 '18 at 14:39
  • No, just some way of understanding if there is any other way of doing this, but I think the @RafaelLima 's answer clarify this. – Pedro Vieira Aug 01 '18 at 14:51

4 Answers4

2

The typical way to prevent polling is by using the Publish/Subscribe pattern.

Your client program will subscribe to the server program and when an event occurs, the server program will publish to all its subscribers for them to handle however they need to.

StuperUser
  • 10,555
  • 13
  • 78
  • 137
  • 1
    publish subscribe doesn't prevent polling!! by subscribing to a topic you still need to check the server if there is something in that topic... unless you are considering only the development overhead, by runtime there is still polling – Rafael Lima Aug 01 '18 at 14:30
  • @RafaelLima In terms of writing specific polling code. Depending on the technology, (e.g. a longstanding GET request or passing a delegate that will be executed in the publish), but that depends on the communication between the publisher and subscriber. – StuperUser Aug 01 '18 at 14:34
  • Yeah, that is the problem that I can't understand. Even in a passive way like this pub/sub method described, it will have yet a polling, checking if there is any in the broker, right? – Pedro Vieira Aug 01 '18 at 14:42
  • @pedrinhow If you provide a way to communicate back to the subscriber as part of the subscribe, you don't have to poll, the publisher will use what's provided at the point of publish. – StuperUser Aug 01 '18 at 14:46
  • @Pedrinhow if you're hung up on stateless nature of HTTP and that the clients and servers are disconnected after HTTP requests complete read up on https://en.wikipedia.org/wiki/Push_technology. I got hung up on how SignalR (as an example of push tech) worked for a while before I read into it. – StuperUser Aug 01 '18 at 14:48
  • I will read that to see if I learn more about this matter, thanks for the help! – Pedro Vieira Aug 01 '18 at 14:56
2

There is no magic... no program will guess when it has new information to be read, what you can do is decide between two approaches,

A -> asks -> B  
A <- is informed <- B

whenever use each? it depends in many other factors like:

1- how fast you need the data be delivered from the moment it is generated? as far as possible? or keep a while and acumulate
2- how fast the data is generated?
3- how many simoultaneuos clients are requesting data at same server
4- what type of data you deal with? persistent? fast-changing?

If you are building something like a stocks analyzer where you need to ask the price of stocks everysecond (and it will change also everysecond) the approach you mentioned may be the best

if you are writing a chat based app like whatsapp where you need to check if there is some new message to the client and most of time wont... publish subscribe may be the best

but all of this is a very superficial look into a high impact architecture decision, it is not possible to get the best by just looking one factor

what i want to show is that

coworkers told me that this method generate some overhead or it's waste of computation

it is not a right statement, it may be in some particular scenario but overhead will always exist in distributed systems

Rafael Lima
  • 3,079
  • 3
  • 41
  • 105
  • I guess you can't run from that, and polling is not a bad practice per say, but you can minimize the overhead with various methods. – Pedro Vieira Aug 01 '18 at 14:48
0

If you flip the order of the requests you end up with something more similar to a standard web API. Your main program (left in your example) would be a server listening for requests. The secondary program would be a client hitting an endpoint on the server to trigger an event.

There's many ways to accomplish this in every language and it doesn't have to be tied to tcp/ip requests.

I'll add a few links for you shortly.

stdlo
  • 55
  • 8
  • Yeah, I read about interruptions that can cause the main program to get that data only if was informed by the secondary program. But how this happen? The main program will need a function to check for interruption right? So it will not end the same way as before? – Pedro Vieira Aug 01 '18 at 14:23
  • I will add this to the question – Pedro Vieira Aug 01 '18 at 14:24
  • Derer to StuperUser's answer. That's the link your looking for. – stdlo Aug 01 '18 at 14:29
0

Well, in most of languages you won't implement such a low level. But theorically speaking, there are different waiting strategies, you are talking about active waiting. Doing this you can easily eat all your memory.

Most of languages implements libraries to allow you to start a process as a service which is at passive waiting and it is triggered when a request comes.

BFajardo
  • 56
  • 3