camunda-community-hub / pyzeebe

Python client for Zeebe workflow engine
https://camunda-community-hub.github.io/pyzeebe/
MIT License
88 stars 37 forks source link

pyzeebe 2.4.0 to 3.0.0 Trouble #239

Open seb-835 opened 3 years ago

seb-835 commented 3 years ago

Hi, i got a worker running in pyzeebe 2.4.0, i made the change to have it using pyzeebe 3.0.0 In my code, i replace :

worker = ZeebeWorker(hostname=gateway.hostname, port=gateway.port, secure_connection=scheme )

by

channel = create_insecure_channel(hostname=gateway.hostname, port=gateway.port)
worker = ZeebeWorker(channel)

and i replace

worker.work() 

by

loop = asyncio.get_running_loop()
loop.run_until_complete(worker.work())

But, the worker failed with error : no running event loop what do i missed ?

kbakk commented 3 years ago

@seb-835 Can you post the full stack trace / output with the error? Also, what version of Python are you running?

seb-835 commented 3 years ago

Hi, i try with python 3.7, 3.8. 3.10 from docker image python:3.7 3.8 3.10 . The traceback is very short :

File "/src/worker.py", line 55, in loop = asyncio.get_running_loop() RuntimeError: no running event loop

i can make a pastebin of the code, there is no secret in.

kbakk commented 3 years ago

@seb-835 Hmm, I don't immediatly see the issue. From the error, it could be something related to what's reported here: https://stackoverflow.com/questions/58774718/asyncio-in-corroutine-runtimeerror-no-running-event-loop

But I don't see how that translates to Pyzeebe.

If you follow the quickstart, does that work?

It would be most helpful if you can create a minimal reproducer.

Andy-JB commented 3 years ago

I think I got around this issue by using loop = asyncio.get_event_loop() Or loop = asyncio.new_event_loop() Instead of loop = asyncio.get_running_loop()

seb-835 commented 3 years ago

Many thanks @kbakk @Andy-JB

One of the workaround give by @Andy-JB work,

if i replace _loop = asyncio.get_runningloop() by _loop = asyncio.get_eventloop() it complains first : DeprecationWarning: There is no current event loop but work.

with loop = asyncio.new_event_loop(), it crash :

RuntimeError: Task <Task pending name='Task-2' coro=<JobPoller.poll() running at /usr/local/lib/python3.10/site-packages/pyzeebe/worker/job_poller.py:28> cb=[gather.<locals>._done_callback() at /usr/local/lib/python3.10/asyncio/tasks.py:718]> got Future <Task pending name='Task-4' coro=<UnaryStreamCall._send_unary_request() running at /usr/local/lib/python3.10/site-packages/grpc/aio/_call.py:557>> attached to a different loop

@kbakk i got a minimal code reproducer if you need,

kbakk commented 3 years ago

See https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_event_loop:

Consider also using the asyncio.run() function instead of using lower level functions to manually create and close an event loop.

See description here for an example of how to use asyncio.run. This is probably what the documentation should use as an example.

Can you test this, @seb-835? Feel free to upload the reproducer here (zip the files) or to a gist, it can be useful for further troubleshooting.

seb-835 commented 3 years ago

@kbakk using asyncio.run(worker.work()) make worker crash too

Task <Task pending name='Task-2' coro=<JobPoller.poll() running at /usr/local/lib/python3.10/site-packages/pyzeebe/worker/job_poller.py:28> cb=[gather.<locals>._done_callback() at /usr/local/lib/python3.10/asyncio/tasks.py:718]> got Future <Task pending name='Task-4' coro=<UnaryStreamCall._send_unary_request() running at /usr/local/lib/python3.10/site-packages/grpc/aio/_call.py:557>> attached to a different loop
Traceback (most recent call last):
  File "/src/worker.py", line 55, in <module>
    asyncio.run(worker.work())
  File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.10/site-packages/pyzeebe/worker/worker.py", line 91, in work
    await self._work_task
  File "/usr/local/lib/python3.10/site-packages/pyzeebe/worker/job_poller.py", line 28, in poll
    await self.activate_max_jobs()
  File "/usr/local/lib/python3.10/site-packages/pyzeebe/worker/job_poller.py", line 32, in activate_max_jobs
    await self.poll_once()
  File "/usr/local/lib/python3.10/site-packages/pyzeebe/worker/job_poller.py", line 47, in poll_once
    async for job in jobs:
  File "/usr/local/lib/python3.10/site-packages/pyzeebe/grpc_internals/zeebe_job_adapter.py", line 23, in activate_jobs
    async for response in self._gateway_stub.ActivateJobs(
  File "/usr/local/lib/python3.10/site-packages/grpc/aio/_call.py", line 320, in _fetch_stream_responses
    message = await self._read()
  File "/usr/local/lib/python3.10/site-packages/grpc/aio/_call.py", line 336, in _read
    await self._preparation

Here is the reproducer code : https://gist.github.com/seb-835/c495fafe0307927e4ed7cd587fa355a2

JonatanMartens commented 3 years ago

I've updated the docs to use get_event_loop instead of get_running_loop. This still feels like a workaround to me, so I'm keeping this issue open

Janiot commented 2 years ago

Hello, it would be nice if you cloud also change it in the README.md, I took me quite a while to figure out the problem and to find this issue. Thank you.