{"id":2581,"date":"2020-01-22T12:34:21","date_gmt":"2020-01-22T12:34:21","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2581"},"modified":"2022-02-17T07:13:48","modified_gmt":"2022-02-17T07:13:48","slug":"prevent-php-function-being-run-on-different-threads","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/threads\/prevent-php-function-being-run-on-different-threads","title":{"rendered":"Prevent PHP function being run on different threads"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Using directory approach <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/----- BLOCK THIS FUNCTION BEING RUN SIMULTANEOUSLY ON DIFFERENT THREADS -----\n  \/\/Creating and deleting a directory is a good way tot do this in PHP because although files may be opened by two different processes (which happily overwrite each other), \n  \/\/directories don\u2019t have such problems (they may be created and removed, but they can\u2019t be written)\n  \/\/We check for the presence of a directory by creating one. If the creation fails, then the directory is already there. If the creation succeed, then, the directory was not there.\n  if (mkdir('\/tmp\/mysemaphorefile.lock', 0700))\n  {\n    \/\/We are OK to run\n  \n\n    rmdir('\/tmp\/mysemaphorefile.lock') ;\n  }\n  else\n  {\n    \/\/Another script is already running\n    \n  }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using directory approach<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[225],"tags":[],"class_list":["post-2581","post","type-post","status-publish","format-standard","hentry","category-threads"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=2581"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2581\/revisions"}],"predecessor-version":[{"id":2582,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2581\/revisions\/2582"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}