mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 10:56:31 +00:00
make python app load a bit eariler, making startup a bit faster
This commit is contained in:
@@ -10,7 +10,7 @@ namespace Azaion.CommonSecurity.Services;
|
||||
|
||||
public interface IResourceLoader
|
||||
{
|
||||
MemoryStream LoadFileFromPython(string fileName);
|
||||
MemoryStream LoadFileFromPython(string fileName, string? folder = null);
|
||||
void StopPython();
|
||||
}
|
||||
|
||||
@@ -25,29 +25,22 @@ public class PythonResourceLoader : IResourceLoader, IAuthProvider
|
||||
private readonly DealerSocket _dealer = new();
|
||||
private readonly Guid _clientId = Guid.NewGuid();
|
||||
|
||||
public User CurrentUser { get; }
|
||||
public User CurrentUser { get; set; }
|
||||
|
||||
public PythonResourceLoader(ApiConfig apiConfig, ApiCredentials credentials, AzaionApiClient api)
|
||||
public PythonResourceLoader()
|
||||
{
|
||||
StartPython(apiConfig, credentials);
|
||||
StartPython();
|
||||
_dealer.Options.Identity = Encoding.UTF8.GetBytes(_clientId.ToString("N"));
|
||||
_dealer.Connect($"tcp://{SecurityConstants.ZMQ_HOST}:{SecurityConstants.ZMQ_PORT}");
|
||||
|
||||
_dealer.SendFrame(MessagePackSerializer.Serialize(new RemoteCommand(CommandType.GetUser)));
|
||||
var user = _dealer.Get<User>();
|
||||
if (user == null)
|
||||
throw new Exception("Can't get user from Auth provider");
|
||||
|
||||
CurrentUser = user;
|
||||
}
|
||||
|
||||
private void StartPython( ApiConfig apiConfig, ApiCredentials credentials)
|
||||
private void StartPython()
|
||||
{
|
||||
using var process = new Process();
|
||||
process.StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = SecurityConstants.AZAION_INFERENCE_PATH,
|
||||
Arguments = $"-e {credentials.Email} -p {credentials.Password} -f {apiConfig.ResourcesFolder}",
|
||||
//Arguments = $"-e {credentials.Email} -p {credentials.Password} -f {apiConfig.ResourcesFolder}",
|
||||
//UseShellExecute = false,
|
||||
//RedirectStandardOutput = true,
|
||||
// RedirectStandardError = true,
|
||||
@@ -59,17 +52,27 @@ public class PythonResourceLoader : IResourceLoader, IAuthProvider
|
||||
process.Start();
|
||||
}
|
||||
|
||||
public void Login(ApiCredentials credentials)
|
||||
{
|
||||
_dealer.SendFrame(RemoteCommand.Serialize(CommandType.Login, credentials));
|
||||
var user = _dealer.Get<User>();
|
||||
if (user == null)
|
||||
throw new Exception("Can't get user from Auth provider");
|
||||
|
||||
CurrentUser = user;
|
||||
}
|
||||
|
||||
public void StopPython()
|
||||
{
|
||||
_dealer.SendFrame(MessagePackSerializer.Serialize(new RemoteCommand(CommandType.Exit)));
|
||||
_dealer?.Close();
|
||||
_dealer.Close();
|
||||
}
|
||||
|
||||
public MemoryStream LoadFileFromPython(string fileName)
|
||||
public MemoryStream LoadFileFromPython(string fileName, string? folder = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dealer.SendFrame(MessagePackSerializer.Serialize(new RemoteCommand(CommandType.Load, fileName)));
|
||||
_dealer.SendFrame(RemoteCommand.Serialize(CommandType.Load, new LoadFileData(fileName, folder)));
|
||||
|
||||
if (!_dealer.TryReceiveFrameBytes(TimeSpan.FromSeconds(3), out var bytes))
|
||||
throw new Exception($"Unable to receive {fileName}");
|
||||
|
||||
Reference in New Issue
Block a user