1

我是这个名为 Goblin 的 Gremlin 驱动程序的新手。我正在关注Goblin - Async Python toolkit的介绍/教程。如文档中所述,我创建了一个 Python 协程:

loop = asyncio.get_event_loop()
app = loop.run_until_complete(loop)
app.register(Person, Knows)
async def go(app):
    session = await app.session()
    leif = Person()
    leif.name = 'Leif'
    leif.age = 28
    jon = Person()
    jon.name = 'Jonathan'
    works_with = Knows(leif, jon)
    session.add(leif, jon, works_with)
    await session.flush()
    result = await session.g.E(works_with.id).next()
    assert result is works_with
    people = session.traversal(Person)  # element class based traversal source
    async for person in people:
         print(person)

当我运行 Python 脚本时,程序似乎陷入了无限循环。此时控制台中既没有显示结果也没有显示错误。

希望任何人都可以帮助我!艾伦

4

1 回答 1

3

很难准确地说出这里发生了什么,因为示例代码不完整,并且包含与 Goblin 代码无关的错误。例如,以下将引发类型错误:

app = loop.run_until_complete(loop)

确保您使用的是 Goblin 2.0.0,运行在 的 Gremlin 服务器 (TinkerPop 3.2.4) localhost:8182,然后尝试以下操作:

import asyncio
from goblin import element, properties, Goblin

# Model definition
class Person(element.Vertex):
    name = properties.Property(properties.String)
    age = properties.Property(properties.Integer)


class Knows(element.Edge):
    notes = properties.Property(properties.String, default='N/A')


# Set up event loop and app
loop = asyncio.get_event_loop()
app = loop.run_until_complete(Goblin.open(loop))

# Register the models with the app
app.register(Person, Knows)

async def go(app):
    session = await app.session()
    leif = Person()
    leif.name = 'Leif'
    leif.age = 28
    jon = Person()
    jon.name = 'Jonathan'
    works_with = Knows(leif, jon)
    session.add(leif, jon, works_with)
    await session.flush()
    result = await session.g.E(works_with.id).next()
    assert result is works_with
    people = session.traversal(Person)
    async for person in people:
        print(person)
    # Make sure to close the app
    await app.close()

# Run the coroutine
loop.run_until_complete(go(app))

这应该会运行,如果没有,请考虑在Github上与我联系,因为我是这个库的作者。

于 2017-05-12T15:17:25.740 回答