1

In a PHP (server) program I have:

file_put_contents("large_file.txt",$logs."\n",FILE_APPEND|LOCK_EX);

and just after this line an echo:

echo json_encode($params);

The first line takes a lot of time to execute, maybe because $logs can contain a very large amount of data.

Is there any way to let file_puts_contents work independently and jump to the next line?

Dave
  • 5,108
  • 16
  • 30
  • 40
3000
  • 187
  • 1
  • 12
  • 2
    No, a php task is single threaded and blocking. You can call another script with an exec. You can find an explanation here: [StackOverflow: asynchronous-shell-exec-in-php](https://stackoverflow.com/questions/222414/asynchronous-shell-exec-in-php) – Thomas Deutschländer Jul 19 '19 at 18:21
  • There is a new function introduced in recent PHP called "swoole_async_writefile". Do google it, can solve your issue. – Ejaz47 Jul 19 '19 at 18:25
  • 2
    @Ejaz47 The PHP function [`swoole_async_writefile()`](https://www.php.net/manual/en/function.swoole-async-writefile.php) is provided by the [Swoole PHP extension](https://pecl.php.net/package/swoole) that is available since 2013 (not quite "recent"). It is not part of the PHP core. One have to install it using PECL in order to use it. The extension is not available on Windows and, most probably, it is not provided by hosting providers. Sure, the OP can install and use it if they host the website on their own server. – axiac Jul 19 '19 at 18:33
  • @ThomasDeutschländer: interesting, thanks! :-) – 3000 Jul 20 '19 at 06:31

0 Answers0