Jump to content
Nefari0

lxml in TPythonThread with Delphi don´t works

Recommended Posts

Hi!

Have a some issue - lxml in TPythonThread with Delphi don´t works but it works in OwnThreadstate with the same script

 

Code: from lxml import html

it calls the error - this module can only be loaded into one interpreter per process

 

And when I try to run Code: BeautifulSoup(html_source,"lxml")

it calls the error - FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

but in OwnThreadstate with the same script works good. 

 

Same way the Code: BeautifulSoup(html_source,"html.parser") in TPythonThread is works, but I need lxml

 

glad to any suggestions

 

Edited by Nefari0
adding info

Share this post


Link to post

Likely just call CoInitialize

    CoInitialize(nil);
    try
      ... your code here
    finally
      CoUninitialize();
    end;

 

Edited by SwiftExpat
Updates based on feeback

Share this post


Link to post
3 hours ago, SwiftExpat said:

Likely just call CoInitialize


    try
      CoInitialize(nil);
     ... your code here
    finally
      CoUninitialize();
    end;

 

This code is wrong. The try is always placed after the resource has been acquired. 

Share this post


Link to post
19 hours ago, David Heffernan said:

This code is wrong

I think I get you point, would you write it like this?

edited above based on feeback

 

Edited by SwiftExpat
remove misleading code

Share this post


Link to post

No. CoInitialize doesn't report errors by throwing exceptions. You are expected to check its return value. This is stated clearly in the documentation for the function which is required reading before using it. 

 

Let's leave error handling to one side for now though. The try finally pattern is like this. 

 

res := acquireResource;

try

  res.doSomething;

finally

  releaseResource(res);

end;

 

The try/finally is protecting the code *between* acquisition and release. Therefore the try must be *after* acquisition. 

 

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×