|
22 | 22 | import threading |
23 | 23 | import traceback |
24 | 24 | import weakref |
25 | | - |
26 | 25 | from collections.abc import MutableMapping |
27 | 26 |
|
28 | 27 | try: |
@@ -71,7 +70,7 @@ def get_event_loop(task): |
71 | 70 | return getattr(task, "_loop", None) |
72 | 71 |
|
73 | 72 |
|
74 | | -class cached_module(): |
| 73 | +class cached_module: |
75 | 74 | def __init__(self, module_path, name=None): |
76 | 75 | self.module_path = module_path |
77 | 76 | self.name = name or module_path |
@@ -123,9 +122,22 @@ def current_thread_id(self): |
123 | 122 | # all other cases where we can obtain a current greenlet, |
124 | 123 | # then it should indicate we are running as a greenlet. |
125 | 124 |
|
126 | | - current = self.greenlet.getcurrent() |
127 | | - if current is not None and current.parent: |
128 | | - return id(current) |
| 125 | + # current = self.greenlet.getcurrent() |
| 126 | + # if current is not None and current.parent: |
| 127 | + # return id(current) |
| 128 | + |
| 129 | + # In this case, the garbage collector could have already |
| 130 | + # collected the greenlet object, so we need to check if the |
| 131 | + # weakref is still alive before we can access the parent |
| 132 | + |
| 133 | + # print(f"cache: {self._cache.data}") |
| 134 | + # print(f"thread id: {thread.get_ident()}") |
| 135 | + if self._cache.data: |
| 136 | + current = self.greenlet.getcurrent() |
| 137 | + # print(f"greenlet id: {id(current)}") |
| 138 | + if current is not None and current.parent: |
| 139 | + # print(f"greenlet id with parent: {id(current)}") |
| 140 | + return id(current) |
129 | 141 |
|
130 | 142 | if self.asyncio: |
131 | 143 | task = current_task(self.asyncio) |
|
0 commit comments