Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add name keyword argument to loop.create_task() #334

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,46 @@ async def coro():
self.assertFalse(isinstance(task, MyTask))
self.loop.run_until_complete(task)

def test_set_task_name(self):
if self.implementation == 'asyncio' and sys.version_info < (3, 8, 0):
raise unittest.SkipTest('unsupported task name')

self.loop._process_events = mock.Mock()

result = None

class MyTask(asyncio.Task):
def set_name(self, name):
nonlocal result
result = name + "!"

def get_name(self):
return result

async def coro():
pass

factory = lambda loop, coro: MyTask(coro, loop=loop)

self.assertIsNone(self.loop.get_task_factory())
task = self.loop.create_task(coro(), name="mytask")
self.assertFalse(isinstance(task, MyTask))
if sys.version_info >= (3, 8, 0):
self.assertEqual(task.get_name(), "mytask")
self.loop.run_until_complete(task)

self.loop.set_task_factory(factory)
self.assertIs(self.loop.get_task_factory(), factory)

task = self.loop.create_task(coro(), name="mytask")
self.assertTrue(isinstance(task, MyTask))
self.assertEqual(result, "mytask!")
self.assertEqual(task.get_name(), "mytask!")
self.loop.run_until_complete(task)

self.loop.set_task_factory(None)
self.assertIsNone(self.loop.get_task_factory())

def _compile_agen(self, src):
try:
g = {}
Expand Down
15 changes: 14 additions & 1 deletion uvloop/loop.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1386,16 +1386,29 @@ cdef class Loop:
"""Create a Future object attached to the loop."""
return self._new_future()

def create_task(self, coro):
def create_task(self, coro, *, name=None):
"""Schedule a coroutine object.

Return a task object.

If name is not None, task.set_name(name) will be called if the task
object has the set_name attribute, true for default Task in Python 3.8.
"""
self._check_closed()
if self._task_factory is None:
task = aio_Task(coro, loop=self)
else:
task = self._task_factory(self, coro)

# copied from asyncio.tasks._set_task_name (bpo-34270)
if name is not None:
try:
set_name = task.set_name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this gain us something over the slightly more readable:

try:
    task.set_name(name)
except AttributeError:
    pass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't mask an AttributeError coming from the set_name implementation. Which is unlikely but can still happen especially with custom task factories.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion and comments!

I would write something like this:

if name is not None and hasattr(task, "set_name"):
    task.set_name(name)

But this is getting into details (try..except overhead? Two O(1) operations?) and stepping away from the copied code, so I think I'll merge as it is now.

except AttributeError:
pass
else:
set_name(name)

return task

def set_task_factory(self, factory):
Expand Down