Skip to content

Commit da8572c

Browse files
author
Roberto De Ioris
committed
start investigating on python threads support
1 parent d0346b4 commit da8572c

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

Source/PythonConsole/Private/SPythonLog.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "Engine/LocalPlayer.h"
88
#include "GameFramework/GameState.h"
99
#include "SSearchBox.h"
10-
10+
//#include "UnrealEnginePython.h"
1111
#define LOCTEXT_NAMESPACE "PythonConsole"
1212

1313

@@ -159,8 +159,6 @@ void SPythonConsoleInputBox::Tick(const FGeometry& AllottedGeometry, const doubl
159159
}
160160

161161

162-
163-
164162
void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::Type CommitInfo)
165163
{
166164
if (CommitInfo == ETextCommit::OnEnter)
@@ -181,6 +179,8 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T
181179
bIgnoreUIUpdate = false;
182180

183181
// Here run the python code
182+
//FUnrealEnginePythonModule UnrealEnginePythonModule = FModuleManager::LoadModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
183+
//UnrealEnginePythonModule.PythonGILAcquire();
184184

185185
PyObject *eval_ret = PyRun_String(TCHAR_TO_UTF8(*ExecString), Py_file_input, main_dict, local_dict);
186186

@@ -199,6 +199,7 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T
199199

200200
if (!value) {
201201
PyErr_Clear();
202+
//UnrealEnginePythonModule.PythonGILRelease();
202203
OnConsoleCommandExecuted.ExecuteIfBound();
203204
return;
204205
}
@@ -211,6 +212,7 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T
211212

212213
if (!msg) {
213214
PyErr_Clear();
215+
//UnrealEnginePythonModule.PythonGILRelease();
214216
OnConsoleCommandExecuted.ExecuteIfBound();
215217
return;
216218
}
@@ -220,13 +222,15 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T
220222
// taken from uWSGI ;)
221223
if (!traceback) {
222224
PyErr_Clear();
225+
//UnrealEnginePythonModule.PythonGILRelease();
223226
OnConsoleCommandExecuted.ExecuteIfBound();
224227
return;
225228
}
226229

227230
PyObject *traceback_module = PyImport_ImportModule("traceback");
228231
if (!traceback_module) {
229232
PyErr_Clear();
233+
//UnrealEnginePythonModule.PythonGILRelease();
230234
OnConsoleCommandExecuted.ExecuteIfBound();
231235
return;
232236
}
@@ -238,6 +242,7 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T
238242
PyObject *ret = PyObject_CallFunctionObjArgs(format_exception, type, value, traceback, NULL);
239243
if (!ret) {
240244
PyErr_Clear();
245+
//UnrealEnginePythonModule.PythonGILRelease();
241246
OnConsoleCommandExecuted.ExecuteIfBound();
242247
return;
243248
}
@@ -256,6 +261,7 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T
256261
}
257262
PyErr_Clear();
258263
}
264+
//UnrealEnginePythonModule.PythonGILRelease();
259265
}
260266

261267
}

Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ void unreal_engine_init_py_module();
77

88
#define LOCTEXT_NAMESPACE "FUnrealEnginePythonModule"
99

10+
11+
void FUnrealEnginePythonModule::PythonGILRelease() {
12+
ue_python_gil = PyEval_SaveThread();
13+
}
14+
15+
void FUnrealEnginePythonModule::PythonGILAcquire() {
16+
PyEval_RestoreThread((PyThreadState *)ue_python_gil);
17+
}
18+
1019
void FUnrealEnginePythonModule::StartupModule()
1120
{
1221
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
@@ -27,13 +36,13 @@ void FUnrealEnginePythonModule::StartupModule()
2736
char *zip_path = TCHAR_TO_UTF8(*FPaths::Combine(*FPaths::GameContentDir(), UTF8_TO_TCHAR("python35.zip")));
2837
PyObject *py_zip_path = PyUnicode_FromString(zip_path);
2938
PyList_Insert(py_path, 0, py_zip_path);
30-
39+
3140
char *scripts_path = TCHAR_TO_UTF8(*FPaths::Combine(*FPaths::GameContentDir(), UTF8_TO_TCHAR("Scripts")));
3241
PyObject *py_scripts_path = PyUnicode_FromString(scripts_path);
3342
PyList_Insert(py_path, 0, py_scripts_path);
3443

3544

36-
UE_LOG(LogPython, Log , TEXT("Python VM initialized: %s"), UTF8_TO_TCHAR(Py_GetVersion()));
45+
UE_LOG(LogPython, Log, TEXT("Python VM initialized: %s"), UTF8_TO_TCHAR(Py_GetVersion()));
3746
UE_LOG(LogPython, Log, TEXT("Python Scripts search path: %s"), UTF8_TO_TCHAR(scripts_path));
3847

3948
if (PyImport_ImportModule("ue_site")) {
@@ -42,6 +51,9 @@ void FUnrealEnginePythonModule::StartupModule()
4251
else {
4352
unreal_engine_py_log_error();
4453
}
54+
55+
// TODO investigate python threads support
56+
//PythonGILRelease();
4557
}
4658

4759
void FUnrealEnginePythonModule::ShutdownModule()
@@ -54,6 +66,6 @@ void FUnrealEnginePythonModule::ShutdownModule()
5466
}
5567

5668
#undef LOCTEXT_NAMESPACE
57-
69+
5870
IMPLEMENT_MODULE(FUnrealEnginePythonModule, UnrealEnginePython)
5971

Source/UnrealEnginePython/Public/UnrealEnginePython.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ class FUnrealEnginePythonModule : public IModuleInterface
1111
{
1212
public:
1313

14+
void PythonGILAcquire();
15+
void PythonGILRelease();
16+
1417
/** IModuleInterface implementation */
1518
virtual void StartupModule() override;
1619
virtual void ShutdownModule() override;
20+
21+
private:
22+
void *ue_python_gil;
1723
};
1824

0 commit comments

Comments
 (0)