Thread Programm beschleunigen (21 answers)
Opened by steve123 at 2012-05-21 21:01

topeg
 2012-05-22 18:56
#158482 #158482
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Um zu testen ob das funktioniert habe ich das Script zu einem Demo umgeschrieben:

more (18.5kb):
Code (perl): (dl )
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;

my $gsc  = File::Spec->rel2abs('wait.pl');
my $sleep=10;
my $ein  ='C:\Lokale Daten\vbv tiffs\Work\Spool';
my $aus  ='C:\Lokale Daten\vbv tiffs\Work\Tiff';

# 10 Prozesse gleichzeitig starten
my $max_run=10;

my @cmd=($gsc,$sleep);

# PID aller laufenden Kindprozesse
my %running;
# Kommandos die auf Ausfühung warten.
my @waiting;

# bendete Kindprozesse terminieren
$SIG{CHLD}=sub{
    my $pid=wait();
    delete($running{$pid});
  };


die("Error open $ein ($!)\n") unless (opendir(my $dh,$ein));

while (my $Eintrag = readdir($dh))
{
  my $infile  = File::Spec->join($ein,$Eintrag);
  next if(-d $infile);
  if($Eintrag =~ /\.ps$/)
  {
    my $outfile = File::Spec->join($aus,$Eintrag.'.tif');

    print "ADD $infile\n";

    my @c=@cmd;
    push(@c,$infile);
    push(@c,$outfile);
    push(@waiting,\@c);
  }
  Start(\%running,\@waiting,$max_run);
}
closedir($dh);

my $last=0;
while(@waiting || %running)
{
  Start(\%running,\@waiting,$max_run);
  select(undef,undef,undef,0.10);

  my $now=time();
  if($now > $last+2)
  {
    $last=$now;
    print "TIME:".localtime()."\n";
    print "WATITING FOR EXEC: ".@waiting."\n" ;
    print "PROCESSES RUNNING: ".keys(%running)."\n";
    print "\n";
  }

  for my $pid (keys(%running))
  {
    next if(kill(0,$pid));
    delete($running{$pid});
  }
}

########################################################################
########################################################################
sub Start
{
  my $run=shift;
  my $wait=shift;
  my $max=shift;

  while(keys(%$run) < $max)
  {
    return unless(@$wait);
    my $cmd=shift(@$wait);

    print "START: ".join(' ',@$cmd)."\n";

    my $pid=fork();
    die("Fork Failed $!\n") unless(defined($pid));
    unless($pid)
    {
      exec(@$cmd);
      die("EXEC Failed");
    }
    $run->{$pid}=1;
  }
}


Im selben Verzeichnis:
wait.pl:
Code (perl): (dl )
1
2
#!/usr/bin/perl
sleep(rand(shift(@ARGV) // 10));



Die Ausgabe im Wareloop läuft über die Zeit und mit kill(0,$pid) teste ich noch zusätzlich ob die Prozesse laufen.

Das Script funktioniert. Es ist also kein Fehler vom Script. Vielmehr vermute ich das die Konvertierung so lange dauert.

Die Änderungen an das richtige Script angewendet:
more (22.2kb):
Code (perl): (dl )
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;

my $gsc = 'C:\Program Files\gs\gs9.05\bin\gswin64c.exe';
my $dpi = '150';
my $txt = '4';
my $bld = '4';
my $ein='C:\Lokale Daten\vbv tiffs\Work\Spool\\';
my $aus='C:\Lokale Daten\vbv tiffs\Work\Tiff\\';

# 10 Prozesse gleichzeitig starten
my $max_run=10;

my @cmd=($gsc,'-q', '-dSAFER', '-dNOPAUSE', '-dBATCH', '-sDEVICE=tiffg4', '-r'.$dpi, '-dTextAlphaBits='.$txt, '-dGraphicsAlphaBits='.$bld);

# PID aller laufenden Kindprozesse
my %running;
# Kommandos die auf Ausfühung warten.
my @waiting;

# bendete Kindprozesse terminieren
$SIG{CHLD}=sub{
    my $pid=wait();
    delete($running{$pid});
  };


die("Error open $ein ($!)\n") unless (opendir(my $dh,$ein));

while (my $Eintrag = readdir($dh))
{
  my $infile  = File::Spec->join($ein,$Eintrag);
  next if(-d $infile);
  if($Eintrag =~ /\.ps$/)
  {
    my $outfile = File::Spec->join($aus,$Eintrag.'.tif');

    print "ADD $infile\n";

    my @c=@cmd;
    push(@c,'-sOutputFile='.$outfile);
    push(@c,'-f', $infile);
    push(@waiting,\@c);
  }
  Start(\%running,\@waiting,$max_run);
}
closedir($dh);

my $last=0;
while(@waiting || %running)
{
  Start(\%running,\@waiting,$max_run);
  select(undef,undef,undef,0.10);

  my $now=int(@waiting/100);
  if($now != $last)
  {
    $last=$now;
    print "TIME:".localtime()."\n";
    print "WATITING FOR EXEC: ".@waiting."\n" ;
    print "PROCESSES RUNNING: ".keys(%running)."\n";
    print "\n";
  }

  my $now=time();
  if($now > $last+2)
  {
    $last=$now;
    print "TIME:".localtime()."\n";
    print "WATITING FOR EXEC: ".@waiting."\n" ;
    print "PROCESSES RUNNING: ".keys(%running)."\n";
    print "\n";
  }

  for my $pid (keys(%running))
  {
    next if(kill(0,$pid));
    delete($running{$pid});
  }
}

########################################################################
########################################################################
sub Start
{
  my $run=shift;
  my $wait=shift;
  my $max=shift;

  while(keys(%$run) < $max)
  {
    return unless(@$wait);
    my $cmd=shift(@$wait);

    print "START: ".join(' ',@$cmd)."\n";

    my $pid=fork();
    die("Fork Failed $!\n") unless(defined($pid));
    unless($pid)
    {
      exec(@$cmd);
      die("EXEC Failed");
    }
    $run->{$pid}=1;
  }
}


Und die beiden Stellen habe ich noch geändert:
Code (perl): (dl )
1
2
    push(@c,'-sOutputFile='.$outfile);
    push(@c,'-f', $infile);

Da keine Shell-Interpretation statt findet braucht man das quoten auch nicht.

View full thread Programm beschleunigen