Question

What is code blocking? How can you solve it? Please describe two example about your solutions?

What is code blocking? How can you solve it? Please describe two example about your solutions?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Following is the answer:

Blocking:
Blocking means, when one process is running then another process is blocked untill first process is finished.When First Process is running then another process is waits till the first process is complete.

A blocking is the one that returns only :

when the task assigned to it is completed or
it is sure that the task cannot be performed or has failed
To generalize the concept further , we say a blocking call defined this way has a Timeout of infinity i.e. the call will return IF and ONLY IF any of the conditions in 1 or 2 is satisfied , and till then the caller will have to wait and the current thread will be blocked.

Ex:
Consider the situation where we need to read the content from a file.

Read the file from the file system Print the content of the file Do some other stuffs
This would comprise of 3 simple steps as depicted above. In above case each step is depending on the result of it’s predecessor. For instance, we cannot print the content of the file unless the file is read. If suppose the operation 1 would have taken 5 sec to complete followed by 2 and 1 sec for the remaining steps, the overall process would take 5 + 2 + 1 = 8 sec to complete. And for this 8 seconds, all the other operations would be blocked.

That is to say, the server would be busy unless the operation completes and therefore would not be able to process other incoming requests.

How to solve:
make callback function to overcome the problem.
When you run first process then it registers a callback event in function and then run some another task, when first process is completed then callback function shows the output.So this process is asynchronous.It avoids code blocking.

example 1:

Read the file from the file system Print the content of the file whenever the file read completes 1.1 2 Do some other stuffs
Code is started reading the file and do not waits for the operation to complete. Instead it registers a callback event in it’s memory, and starts doing other stuffs. Once the file reading is complete, then it remembers the callback it registered lately for this operation, so it calls that callback and therefore print the content of the file.

This approach is rather sophisticated as there are no stuffs that’s blocking the execution cycle of the program and the server is free almost every time. Well, this is good. The more the time server is free the more the number of incoming requests it can handle.

example 2:

we have a collaborative application sending emails to users when other users comment on something. Let’s say for arguments sake we have 50 separate emails to send. If we use similar code when someone posts a comment processing will be blocked until all 50 emails have been sent.

So our app can be slowed by:

The time it takes to process 50 emails
A slow network to the mail server
Heavy load on the mail server
Until we return from this the slowness is passed directly to the user.

Increasingly web applications do not exist on a single server. They are becoming complex networked applications using cloud infrastructures, third-party APIs and services across the internet.

Solution:

In the email example we can use something like mail api to perform these tasks asynchronously. We bring up resque workers as separate processes and use Redis to store bits of data that the workers can use to do their work. It is a great library and very easy to use. Instead of forcing the user to wait until mail is sent we put the task onto a queue and get a worker to execute it.

Add a comment
Know the answer?
Add Answer to:
What is code blocking? How can you solve it? Please describe two example about your solutions?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT