Multiple stdin / stdout actions during a single process call

I use the Google Closure compiler to compile javascript automatically using PHP (you need to do it this way - in PHP, no security restrictions on a Windows machine). I wrote a simple PHP script that calls a process, passes the .js content to stdin, and receives the recompiled .js through stdout. It works great, the problem is that when I compile, for example, 40.js files, it takes a solid machine for almost 2 minutes. However, the mayor's delay is due to the fact that java launches a new instance of the .jar application for each script. Is there a way to modify the script below to create only one process and send / receive .js content several times before the process ends?

function compileJScript($s) { $process = proc_open('java.exe -jar compiler.jar', array( 0 => array("pipe", "r"), 1 => array("pipe", "w")), $pipes); if (is_resource($process)) { fwrite($pipes[0], $s); fclose($pipes[0]); $output = stream_get_contents($pipes[1]); fclose($pipes[1]); if (proc_close($process) == 0) // If fails, keep $s intact $s = $output; } return $s; } 

I see several options, but I don’t know if this is possible and how to do it:

  • Create the process once and recreate only channels for each file
  • Make java save JIT-ed.jar in memory for faster re-execution
  • If PHP cannot do this, you can use a bridge (another .exe file that will be run every time, transfer stdin / out and redirect it to the running compiler, if something like this even exists)
+5
source share
2 answers

It really is a matter of coordination between the two processes.

Here I wrote a quick 10 minute script (just for fun) that starts the JVM and sends an integer value that java parses and returns incremented .. which PHP will just send back ad-infinitum ..

Php.php

 <?php echo 'Compiling..', PHP_EOL; system('javac Java.java'); echo 'Starting JVM..', PHP_EOL; $pipes = null; $process = proc_open('java Java', [0 => ['pipe', 'r'], 1 => ['pipe', 'w']], $pipes); if (!is_resource($process)) { exit('ERR: Cannot create java process'); } list($javaIn, $javaOut) = $pipes; $i = 1; while (true) { fwrite($javaIn, $i); // <-- send the number fwrite($javaIn, PHP_EOL); fflush($javaIn); $reply = fgetss($javaOut); // <-- blocking read $i = intval($reply); echo $i, PHP_EOL; sleep(1); // <-- wait 1 second } 

Java.java

 import java.util.Scanner; class Java { public static void main(String[] args) { Scanner s = new Scanner(System.in); while (s.hasNextInt()) { // <-- blocking read int i = s.nextInt(); System.out.print(i + 1); // <-- send it back System.out.print('\n'); System.out.flush(); } } } 

To run the script, just put these files in one folder and do

 $ php PHP.php 

you should see how numbers are printed:

 1 2 3 . . . 

Note that although these numbers are printed by PHP, they are actually generated by Java

+5
source

I don’t think that No. 1 from your list is possible, because compiler.jar should have built-in support to support the process live, which is not (and if you think that the compression algorithm requires all the input before it can start processing the data, it makes sense, that the process does not survive).

According to Anyway, to increase the startup speed of the JVM JVM? some people have been able to reduce jvm startup time with nailgun

Nailgun is a client, protocol, and server for running Java programs from the command line without the overhead of starting a JVM. Programs running on the server (which are implemented in Java) and initiated by a client (written in C) that processes all I / O operations.

0
source

Source: https://habr.com/ru/post/1202201/


All Articles