Note: I am getting this error in unit testing. Haven't been able to deploy it to dev/prod. could it be anything to do with app context? how to create one in py.test?
I recently converted a code which was doing multiple io calls to multithreaded. I used gevent to do so. Here is simplified version of the code (removing all necessary identifiable information)
ge_results = {}
if random_condition_is_true():
ge_results['first_key'] = Greenlet.spawn(another_service.fetch_data, ids, another_id)
ge_results['second_key'] = Greenlet.spawn(some_other_method, ids)
if another_condition_is_true():
ge_results['third_key'] = Greenlet.spawn(third_method_call_which_returns_two_values, ids=ids)
results = gevent.joinall(list(ge_results.values()))
if 'first_key' in ge_results:
first_key = ge_results['first_key'].value
if 'third_key' in ge_results:
third_data_set = ge_results['third_key'].value[1]
The issue is happening on third_data_set
. The third_method_call_which_returns_two_values
method return 2 values, and i am trying to assign second value to third_data_set
but i am unable to do so.
When i look into this value using pdb, I see these values
(Pdb) xx = greenlets_result_map['third_key']
(Pdb) xx
<Greenlet at 0x7f3350203a70: third_method_call_which_returns_two_values(AA1(..., qwe=[d,d,d,d], data_i_want=[DataIWant(BB=BB1(...)>
Here it is visible that I receive a Greenlet
object which has AA1
object. Which then has data_i_want
. But i am unable to get it.
When i do xx.value
it says NoneType
. If i try xx[1].value
, i get error that its not subscriptable
I am not sure how to get this value.
Just to be clear, methods which are returning only one value are working perfectly fine with greenlets_result_map['key'].value
code.
Update
An interesting thing that i have noticed that if a method has been called then the greenlet has _run
value otherwise method name.
For example, here is the output of ge_results['first_key']
(Pdb) ge_results['first_key']
(Pdb) <Greenlet at 0x7f3350203830: _run>
See there is _run
here. But here is the output for ge_results['second_key']
(Pdb) ge_results['second_key']
(Pdb) <Greenlet at 0x7f3350203950: some_other_method([])>
See it doesn't have _run
. Am i not joining them all?