B4J Question [PyBridge] Waiting For (Py.Flush) Complete (Success As Boolean) doesn't seem to Wait

William Lancee

Well-Known Member
Licensed User
Longtime User
This is just the example as shown in the PyBridge tutorial.
Does anyone have any idea why the Wait For here doesn't work? Is Complete set somehow?
I tested Py.Flush.Completed but it is false.

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Py.Initialize(Me, "Py")
    Dim opt As PyOptions = Py.CreateOptions("Python/python/python.exe")
    Py.Start(opt)
    Wait For Py_Connected (Success As Boolean)
    If Success = False Then
        LogError("Failed to start Python process.")
        Return
    End If
    PrintPythonVersion
    
    Dim Tuple As PyWrapper = Py.WrapObject(Array(1, 2, 3, 4, 5))
    Tuple.Print
    Tuple.TypeOf.Print2("type is:", "", False)
    Dim list As PyWrapper = Tuple.ToList
    list.Run("append").Arg(6)
    list.Print
    Wait For (Py.Flush) Complete (Success As Boolean)        'NOT waiting
    Log("after " & Success)
    Log(Py.Flush.Completed)
End Sub

'Logs:
'Server Is listening on port: 62118
'Python path: C:\Program Files\Anywhere Software\B4J\libraries\Python\python\python.exe
'Call B4XPages.GetManager.LogEvents = True To enable logging B4XPages events.
'connected
'starting PyBridge v1.00
'watchdog set To 30 seconds
'Connecting To port: 62118
'after true
'false
'Python version: 3.12.8 (tags/v3.12.8:2dc476b, Dec  3 2024, 19:30:04) [MSC v.1942 64 Bit (AMD64)]
'(1, 2, 3, 4, 5)
'type is: <class 'tuple'>
'[1, 2, 3, 4, 5, 6]
 
Solution
You are not alone.
I also experience the same but it happens randomly.

As stated by Erel, python and B4J (Java) are running on their own threads.

I think by using wait for still cannot guarantee the logs will appear in order.

Maybe something happening after the Flush method and jShell.

I understand that it should be the last thing pywrapper finish all the python code and close the object.

If this doesn't affect the execution of python code then I think we don't need to worry too much.

I am new to pybridge so I can be wrong.

William Lancee

Well-Known Member
Licensed User
Longtime User
My problem existed before I added that log line to debug the code.
Why does the log appear before print the statements?
B4X:
    Dim Tuple As PyWrapper = Py.WrapObject(Array(1, 2, 3, 4, 5))
    Tuple.Print
    Tuple.TypeOf.Print2("type is:", "", False)
    Dim list As PyWrapper = Tuple.ToList
    list.Run("append").Arg(6)
    list.Print
    Dim rs As ResumableSub = Py.Flush
    Wait For (Py.Flush) Complete (Success As Boolean)        'It is waiting or is it?
    Log(rs.Completed)
    
'starting PyBridge v1.00
'watchdog set to 30 seconds
'Connecting to port: 51030
'true        <====
'Python version: 3.12.8 (tags/v3.12.8:2dc476b, Dec  3 2024, 19:30:04) [MSC v.1942 64 bit (AMD64)]
'(1, 2, 3, 4, 5)
'type is: <class 'tuple'>
'[1, 2, 3, 4, 5, 6]
'PyBridge disconnected
'Process completed. ExitCode: 1
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
list.Print
'Wait For (Py.Flush) Complete (Success As Boolean)        'NOT waiting
'Log("after " & Success)
'Log(Py.Flush.Completed)
Dim rs As ResumableSub = Py.Flush
Wait For (Py.Flush) Complete (Success As Boolean)        'It is waiting
Log("after " & Success)
Log(rs.Completed)

I get this Logs:
B4X:
Waiting for debugger to connect...
Program started.
Server is listening on port: 50541
Python path: C:\Program Files\B4X\B4J\libraries\Python\python\python.exe
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
connected
starting PyBridge v1.00
watchdog set to 30 seconds
Connecting to port: 50541
Python version: 3.12.8 (tags/v3.12.8:2dc476b, Dec  3 2024, 19:30:04) [MSC v.1942 64 bit (AMD64)]
(1, 2, 3, 4, 5)
type is: <class 'tuple'>
[1, 2, 3, 4, 5, 6]
after true
true
PyBridge disconnected
Process completed. ExitCode: 1
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Thank you @aeric for testing. I copied your code and I get these logs!
The thing is, I have exactly the same code as you. Are you using the Local or Global Python?

B4X:
Waiting for debugger to connect...
Program started.
Server is listening on port: 53002
Python path: C:\Program Files\Anywhere Software\B4J\libraries\Python\python\python.exe
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
connected
starting PyBridge v1.00
watchdog set to 30 seconds
Connecting to port: 53002
after true
true
Python version: 3.12.8 (tags/v3.12.8:2dc476b, Dec  3 2024, 19:30:04) [MSC v.1942 64 bit (AMD64)]
(1, 2, 3, 4, 5)
type is: <class 'tuple'>
[1, 2, 3, 4, 5, 6]

My code
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Py.Initialize(Me, "Py")
    Dim opt As PyOptions = Py.CreateOptions("Python/python/python.exe")
    Py.Start(opt)
    Wait For Py_Connected (Success As Boolean)
    If Success = False Then
        LogError("Failed to start Python process.")
        Return
    End If
    PrintPythonVersion
    
    Dim Tuple As PyWrapper = Py.WrapObject(Array(1, 2, 3, 4, 5))
    Tuple.Print
    Tuple.TypeOf.Print2("type is:", "", False)
    Dim list As PyWrapper = Tuple.ToList
    list.Run("append").Arg(6)
    list.Print
    'Wait For (Py.Flush) Complete (Success As Boolean)        'NOT waiting
    'Log("after " & Success)
    'Log(Py.Flush.Completed)
    Dim rs As ResumableSub = Py.Flush
    Wait For (Py.Flush) Complete (Success As Boolean)        'It is waiting - NOT for me
    Log("after " & Success)
    Log(rs.Completed)
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I am using JDK19.
I switched to JDK14 and get something similar to yours.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I am also using C:\Java\jdk-19.0.2\bin\javac.exe.
Maybe it is waiting in some other process. Do you know a way to check?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Using your code I get
Waiting for debugger to connect...
Program started.
Server is listening on port: 54139
Python path: D:\python313\python.exe
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
connected
starting PyBridge v1.00
watchdog set to 30 seconds
Connecting to port: 54139
Python version: 3.13.7 (tags/v3.13.7:bcee1c3, Aug 14 2025, 14:15:11) [MSC v.1944 64 bit (AMD64)]
(1, 2, 3, 4, 5)
type is: <class 'tuple'>
[1, 2, 3, 4, 5, 6]
after true
true
PyBridge disconnected
Process completed. ExitCode: 1
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I also tested with JDK11 which behave similar to JDK19.

Edit: Now no matter which JDK, I get what you get. 😆

1767888169999.png
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
maybe try making the python code a separate sub and just call from B4XPage_Created like
B4X:
Sub makeitseparate
    Dim Tuple As PyWrapper = Py.WrapObject(Array(1, 2, 3, 4, 5))
    Tuple.Print
    Tuple.TypeOf.Print2("type is:", "", False)
    Dim list As PyWrapper = Tuple.ToList
    list.Run("append").Arg(6)
    list.Print
    Dim rs As ResumableSub = Py.Flush
    Wait For (Py.Flush) Complete (Success As Boolean)        'It is waiting - NOT for me
    Log("after " & Success)
    Log(rs.Completed)
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
It seems it happens randomly on each Debug compilation.
Clean project does help.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Using the Global Python gives me the expected result. But only once. Then it reverts to the no Wait result.
@Daestrum I tried the separate Sub idea. No luck.

I have restarted my computer.
I have tried release and debug modes
I have tried Global and Local Python - inconsistent results

When I was working on some AI projects recently, I had to specify a kernel and the number of cores to use.
I wonder if that may have changed some CPU setting in an unexpected way?

First I'll clean the project.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
After Clean project I get this error. Any idea why?

B4X:
Server is listening on port: 53464
Python path: C:\Users\willi\AppData\Local\Programs\Python\Python310\python.exe
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
connected
starting PyBridge v1.00
watchdog set to 30 seconds
Connecting to port: 53464
after true
true
Python version: 3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
(1, 2, 3, 4, 5)
type is: <class 'tuple'>
[1, 2, 3, 4, 5, 6]
[IDE message - 11:28:07]
An error occurred.
The process cannot access the file 'C:\Users\willi\Desktop\testPy\B4J\Objects\testPy.jar' because it is being used by another process.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
After Clean project I get this error. Any idea why?

B4X:
Server is listening on port: 53464
Python path: C:\Users\willi\AppData\Local\Programs\Python\Python310\python.exe
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
connected
starting PyBridge v1.00
watchdog set to 30 seconds
Connecting to port: 53464
after true
true
Python version: 3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
(1, 2, 3, 4, 5)
type is: <class 'tuple'>
[1, 2, 3, 4, 5, 6]
[IDE message - 11:28:07]
An error occurred.
The process cannot access the file 'C:\Users\willi\Desktop\testPy\B4J\Objects\testPy.jar' because it is being used by another process.
It seems the jar is not kill or quit properly.
You should stop the app (Kill Process) before Clean project.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Have you tried create a console (non-UI) or server app?
Can it be UI app run on different thread?
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
OK Did kill, no more error messages. Just the same old problem of No Wait.
So far out of about 50 tries it worked twice, but not in a consistent way.
Changing to Global Python worked once, then no more.

Must be a timing issue.

I'll try non-UI next
 
Upvote 0
Top