Asyncio and ontology

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Asyncio and ontology

cdemir
Hello,

I would like to combine asyncio with owlready2 so that the elapsed time during the dynamic class generation is minimised. I was wondering whether you (1) foresee any issue with it, (2) any suggestion before delving into modifying the source code.

Cheers,
Caglar
Reply | Threaded
Open this post in threaded view
|

Re: Asyncio and ontology

cdemir
I have modified namespace.py by including following functions into Ontology Class.

async def __aexit__(self, exc_type=None, exc_val=None, exc_tb=None):
      Namespace.__exit__(self, exc_type, exc_val, exc_tb)
      if not self.loaded:
        self.loaded = True
        if self.graph: self.graph.set_last_update_time(time.time())
def __await__(self):
    return new_sleep().__await__()
def __aenter__(self):
    if self.ontology is None: raise ValueError("....")
    if self.world.graph: self.world.graph.acquire_write_lock()
    #CURRENT_NAMESPACES.append(self)
    l = CURRENT_NAMESPACES.get()
    if l is None: CURRENT_NAMESPACES.set([self])
    else:         l.append(self)
    return self

# Outside of Ontology class
import asyncio
async def new_sleep():
  await asyncio.sleep(0)


This implementation appears to allow  
async with my_ontology:
... NewClass = types.new_class("NewClassName", (SuperClass,))

I plan to benchmark this implementation to evaluate possible runtime gains. The question is whether such inclusion introduces bugs in _GraphManager or elsewhere under the hood.

Reply | Threaded
Open this post in threaded view
|

Re: Asyncio and ontology

Jiba
Administrator
In reply to this post by cdemir
Hello,

I never tried this. I foresee some issues with the use of "with onto: ..." blocks in Owlready.

For example in that code:


loop = asyncio.get_event_loop()

with onto:
  loop.call_soon(my_func, loop)

The loop will actually call my_func() OUTSIDE of the "with onto: ..." block.

Similarly, when using coroutines, Python does not exit/enter "with" blocks when switching from a coroutine to another one. This may cause troubles with the "write locks" on the database.

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Asyncio and ontology

Jiba
Administrator
In reply to this post by cdemir
Oh, it seems that Python has something to work around the problem I mentioned on the "with" blocks in my previous reply! Good to know.

_GraphManager basically delegates everything to sqlite3, wo it will highly depend on it.

Please keep me in touch with the results.

Jiba