Why does this Python script hang? async-await? Django-ORM?

27 views Asked by At

I'm trying to learn to use this Python package for patent searching:

https://pypi.org/project/patent-client/

https://patent-client.readthedocs.io/en/latest

I'm following this example:

https://patent-client.readthedocs.io/en/latest/examples/3%20-%20Company%20Ownership.html

When I paste the example into a simple script like below it hangs after execution. The print functions all execute as if the code worked properly but the script never terminates.

The patent_client emphasises async-await and Django-ORM compatible API. Is there something I need to add to get some async process to 'hang up'?? (sorry for wrong terminology)

from patent_client import USApplication, Assignment

company_name = 'Luminopia'

print("Company name: {}".format(company_name))
print("Getting applications")
applicant_apps = USApplication.objects.filter(first_named_applicant=company_name).values_list('appl_id', flat=True).to_list()
print("Found ", end='')
print("{} applications".format(len(applicant_apps)))
print("Getting assignments")
assigned_apps = Assignment.objects.filter(assignee=company_name).explode('properties').values_list('appl_id', flat=True).to_list()
print("Found ", end='')
print("{} assignments".format(len(assigned_apps)))

I added the print functions to verify where the script is hanging but the last line seems to execute fine. It just never returns to the command prompt.

Running in Python 3.10.

The terminal output from the script is:

Company name: Luminopia
Getting applications
Found 3 applications
Getting assignments
Found 4 assignments

I tried adding exit() to the end of the program but to no avail.

I added print(*threading.enumerate(), sep = "\n" and the output is below. How do I tell these threads to terminate?

<_MainThread(MainThread, started 140393979326464)>
<Thread(asyncio_0, started 140393733486144)>
<WorkerThread(AnyIO worker thread, started 140393725093440)>
<Thread(asyncio_0, started 140393617290816)>
<Thread(asyncio_0, started 140393608898112)>
<Thread(asyncio_0, started 140393600505408)>
<Thread(asyncio_0, started 140393592112704)>
<Thread(asyncio_0, started 140393583720000)>
<Thread(asyncio_0, started 140393575327296)>
<Thread(asyncio_0, started 140393566934592)>
<Thread(asyncio_0, started 140393018553920)>
<Thread(asyncio_0, started 140393010161216)>
<Thread(asyncio_0, started 140393001768512)>
<Thread(asyncio_0, started 140392993375808)>
<Thread(asyncio_0, started 140392984983104)>
<Thread(asyncio_0, started 140392976590400)>
<WorkerThread(AnyIO worker thread, started 140392968197696)>
<WorkerThread(AnyIO worker thread, started 140392481683008)>
0

There are 0 answers