{"id":7919,"date":"2019-11-08T10:09:22","date_gmt":"2019-11-08T01:09:22","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=7919"},"modified":"2019-11-06T10:14:51","modified_gmt":"2019-11-06T01:14:51","slug":"synchronous-vs-asynchronous","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/7919","title":{"rendered":"Synchronous VS Asynchronous"},"content":{"rendered":"\n<p>If two processes are running Synchronously, it means that second process will wait until or unless first process complete.<\/p>\n\n\n\n<p>If two processes are running Asynchronously, it means second process will not wait for first process result and will be executed.<\/p>\n\n\n\n<p>I will explain a bit more detail in below for each communication.<\/p>\n\n\n\n<h4>Synchronous Communication<\/h4>\n\n\n\n<p>If two processes are running&nbsp;<code>Synchronously<\/code>, it means that second process will wait until or unless first process complete. Synchronous Transmission is efficient, reliable and is used for transferring a large amount of data. It provides real-time communication between connected devices. Chat Rooms, Video Conferencing, telephonic conversations, as well as face to face interactions, are some of the examples of Synchronous Transmission.<\/p>\n\n\n\n<p>Check the sample php snippet below for synchronous communication.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\necho(\u201cBefore the first file is read.\u201d);\n$fileContents = file_get_contents(\u201csample.txt\u201d);\n\n\/\/You can\u2019t do anything while the file is being read, your script is \u2018stuck\u2019.\n\necho(\u201cAfter the first file has completed reading.\u201d);\n\necho(\u201cBefore the second file is read.\u201d);\n\n$fileContents = file_get_contents(\u201csample2.txt\u201d);\n\/\/You can\u2019t do anything while the file is being read, your script is \u2018stuck\u2019.\necho(\u201cAfter the second file has completed reading.\u201d);\n\n==========================\nOutput always looks like this:\nBefore the first file is read.\nAfter the first file has completed reading.\nBefore the second file is read.\nAfter the second file has completed reading.<\/code><\/pre>\n\n\n\n<h4>Asynchronous Communication<\/h4>\n\n\n\n<p>If two processes are running&nbsp;<code>Asynchronously<\/code>, it means second process will not wait for second process result and will be executed. It is simple, fast, economical and does not require a 2-way communication. Letters, emails, forums, televisions and radios are some of the examples of Asynchronous Transmission.<\/p>\n\n\n\n<p>Check the js snippet below for asynchronous communication<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(\u201cBefore the first file is read.\u201d);\n\nhypotheticalFileGetContents(\u201csample.txt\u201d, function(fileContents){\n\/\/fileContents now contains the file contents, this function is only called when the file read in the background has finished\nconsole.log(\u201cAfter the first file has completed reading.\u201d);\n});\n\n\/\/You\u2019ve now told it to start the first read, but it won\u2019t \u2018block\u2019 your script execution. It will do the read in the background, and immediately move on with the rest of your code.\n\nconsole.log(\u201cBefore the second file is read.\u201d);\n\nhypotheticalFileGetContents(\u201csample2.txt\u201d, function(fileContents){\n\/\/fileContents now contains the file contents, this function is only called when the file read in the background has finished\nconsole.log(\u201cAfter the second file has completed reading.\u201d);\n});\n\n=======================\nOutput could look something like this:\nBefore the first file is read.\nBefore the second file is read.\nAfter the first file has completed reading.\nAfter the second file has completed reading.<\/code><\/pre>\n\n\n\n<p>By Yuuma<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/7919\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/7919\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Synchronous VS Asynchronous\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/7919\" data-text=\"Synchronous VS Asynchronous\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/7919\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/7919\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>If two processes are running Synchronously, it means that second process will wait until or unless first proce [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/7919"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=7919"}],"version-history":[{"count":4,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/7919\/revisions"}],"predecessor-version":[{"id":7924,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/7919\/revisions\/7924"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=7919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=7919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=7919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}