Using an externally started, already running Python Bridge instance

Normally GT controls the starting and stopping of the Python process as described before. It is also possible to skip this part. The infrastructure will then assume the Python process is already up and running.

Warning This is a technical approach not needed for normal usage. It might be instructive to learn more about the internals.

To do this we need to reconfigure the infrastracture with the following expression.

PBPlatform current setManualProcess

This switches the class responsible for controlling the Python process to LanguageLinkManualProcess LanguageLinkAbstractProcess subclass: #LanguageLinkManualProcess instanceVariableNames: '' classVariableNames: '' package: 'PharoLink-Pharo-Processes' which is effectively a dummy. I.e. it assumes the process is already running.

We create the bridge but do not yet start it.

myPythonBridge := PBApplication new

Note how, in the Details tab you can already see that the server side is assumed to be running, Setting debug mode will add logging to the Python process which is useful to see what is happening.

myPythonBridge debugMode: true

To start the Python process manually, go into the PythonBridge directory. Note that the setup which happens automatically when starting in the normal way needs to have run before !

PBPlatform current defaultSettings workingDirectory

This is where you should execute the command to start Python. There is a helper method that generates this command, picking up the correct ports.

myPythonBridge settings manualCommand

You might need to further edit this for you situation. The executable might be different, you might need to activate an environment, or use pipenv run python (this was also earlier explained at the end of the troubleshooting section).

Now we can finally start the GT side.

myPythonBridge start

And we can use it like before.

myPythonBridge newCommandStringFactory
	script: 'import random';
	resultExpression: 'list(map(lambda x: random.randrange(99)+1, range(1,10)))';

With this approach we can start and stop from the GT side, connecting and disconnecting, multiple times. The Python process keeps on running. This is useful for heavy or long running setups.

myPythonBridge stop

You can send an explicit quit request to the Python instance.

myPythonBridge newCommandFactory<< #quit call; send; yourself

After such a forced quit, it is best to also stop the bridge on the GT to release all network connections from all sides.

After this experiment, do not forget to reset the global configuration to get back to the normal situation.

PBPlatform reset