Bonjour,
Je doit dans le cadre d'un projet pouvoir appliquer un traitement en temps réel la sortie d'une commande externe en PHP. Jusque là pas de problème plusieurs méthodes existent et fonctionnent. Le problème vient lorsque j'essaie de gérer les erreurs potentiels qui peuvent survenir pendant ce traitement, pour que ce soit plus explicite j'ai le code suivant :
Testé avec les 2 fonctions j'aurais la sortie suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 <?php error_reporting(E_ALL); ini_set("display_errors", 1); function read_command($command) { echo 'Starting read_command' . PHP_EOL; try { $command_process = popen($command, 'r'); while (!feof($command_process)) { $output_command = fgets($command_process); echo $output_command . PHP_EOL; process_output_command($output_command); flush(); } } catch (Exception $e) { echo 'Re-throwing exception' . PHP_EOL; throw $e; } finally { echo 'Finally block' . PHP_EOL; pclose($command_process); } } function read_command_pipes($command) { echo 'Starting read_command_pipes' . PHP_EOL; $descriptors_spec = [ 0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w'] ]; flush(); $command_process = proc_open($command, $descriptors_spec, $pipes, realpath('./'), []); if (is_resource($command_process)) { try { while ($output_command = fgets($pipes[1])) { echo $output_command . PHP_EOL; flush(); process_output_command($output_command); } } catch (Exception $e) { echo 'Re-throwing exception' . PHP_EOL; throw $e; } finally { echo 'Finally block' . PHP_EOL; fclose($pipes[0]); fclose($pipes[1]); fclose($pipes[2]); proc_close($command_process); } } } function process_output_command($output_command) { echo 'Throwing exception' . PHP_EOL; throw new Exception('Exception process'); } try { //read_command('ping google.fr'); read_command_pipes('ping google.fr'); } catch (Exception $e) { echo 'Main exception catch' . PHP_EOL; exit(1); }
Et ensuite le processus passe en attente et je suis obligé de le tuer manuellement. Mon objectif est d'arriver jusqu'au bloc catch du script principal et atteindre le exit(1) pour finir la phase actuelle du projet. Je n'ai plus d'idées et j'ai besoin de pistes neuves pour résoudre ce problème.Starting read_command_pipes
PING google.fr (216.58.211.67) 56(84) bytes of data.
Throwing exception
Re-throwing exception
Finally block
Merci d'avance à tous ceux qui s'y pencheront dessus et qui auront des propositions.
Partager