forked from Embarcadero/python4delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit1.pas
More file actions
109 lines (92 loc) · 2.83 KB
/
Unit1.pas
File metadata and controls
109 lines (92 loc) · 2.83 KB
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
109
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo, FMX.StdCtrls,
PythonEngine, PythonVersions, System.SyncObjs;
type
TForm1 = class(TForm)
memoIn: TMemo;
PythonEngine: TPythonEngine;
Splitter1: TSplitter;
memoOut: TMemo;
Panel1: TPanel;
Button1: TButton;
PythonInputOutput: TPythonInputOutput;
procedure Button1Click(Sender: TObject);
procedure PythonInputOutputSendUniData(Sender: TObject; const Data: string);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FOutputStream: TMemoryStream;
FCriticalSection : TCriticalSection;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
begin
PythonEngine.ExecString(UTF8Encode(MemoIn.Text));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FOutputStream := TMemoryStream.Create;
FCriticalSection := TCriticalSection.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FOutputStream.Free;
FCriticalSection.Free;
end;
procedure TForm1.PythonInputOutputSendUniData(Sender: TObject;
const Data: string);
{
FMX TMemo is slow when adding many lines one by one.
To avoid this we accumulate output and print it in one go.
The critical section allows for multiple threads to print.
If you use this code then set PythonInputOuput.RawOutput to True
A simpler solution would be to set RawOuput to False and then do
MemoOut.Lines.Add(Data);
MemoOut.GoToTextEnd;
This should be good enough if python does not produce a massive
amount of output.
}
Var
ScheduleWrite: Boolean;
begin
if Data = '' then Exit;
fCriticalSection.Enter;
try
ScheduleWrite := FOutputStream.Size = 0;
FOutputStream.Write(Data[1], Length (Data) * 2);
if ScheduleWrite then
TThread.ForceQueue(nil, procedure
var
WS: string;
begin
fCriticalSection.Enter;
try
if fOutputStream.Size > 0 then begin
SetLength(WS, fOutputStream.Size div 2);
fOutputStream.Position := 0;
fOutputStream.Read(WS[1], Length(WS) * 2);
fOutputStream.Size := 0;
if (MemoOut.Lines.Count > 0) and (MemoOut.Lines[MemoOut.Lines.Count -1] <> '') then
MemoOut.Lines.Add('');
MemoOut.Text := MemoOut.Text + WS;
MemoOut.GoToTextEnd;
end;
finally
fCriticalSection.Leave;
end;
end);
finally
fCriticalSection.Leave;
end;
end;
end.