Skip to content

Commit

Permalink
Revert "AsynchronousTask: add coroutine async_start method"
Browse files Browse the repository at this point in the history
This reverts commit d66e9ec.

Bug: https://bugs.gentoo.org/716636
Signed-off-by: Zac Medico <zmedico@gentoo.org>
  • Loading branch information
zmedico committed Apr 8, 2020
1 parent 62ee9ec commit 71ae5a5
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 88 deletions.
22 changes: 5 additions & 17 deletions lib/_emerge/AsynchronousTask.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Copyright 1999-2020 Gentoo Authors
# Copyright 1999-2018 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import signal

from portage import os
from portage.util.futures import asyncio
from portage.util.futures.compat_coroutine import coroutine, coroutine_return
from portage.util.SlotObject import SlotObject

class AsynchronousTask(SlotObject):
Expand All @@ -23,28 +22,13 @@ class AsynchronousTask(SlotObject):

_cancelled_returncode = - signal.SIGINT

@coroutine
def async_start(self):
self._start_hook()
yield self._async_start()

@coroutine
def _async_start(self):
self._start()
coroutine_return()
yield None

def start(self):
"""
Start an asynchronous task and then return as soon as possible.
"""
self._start_hook()
self._start()

def _start(self):
self.returncode = os.EX_OK
self._async_wait()

def async_wait(self):
"""
Wait for returncode asynchronously. Notification is available
Expand All @@ -65,6 +49,10 @@ def async_wait(self):
self._async_wait()
return waiter

def _start(self):
self.returncode = os.EX_OK
self._async_wait()

def isAlive(self):
return self.returncode is None

Expand Down
17 changes: 3 additions & 14 deletions lib/_emerge/CompositeTask.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# Copyright 1999-2020 Gentoo Authors
# Copyright 1999-2018 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

from _emerge.AsynchronousTask import AsynchronousTask
from portage import os
from portage.util.futures import asyncio


class CompositeTask(AsynchronousTask):

Expand Down Expand Up @@ -100,7 +98,7 @@ def _default_final_exit(self, task):
def _start_task(self, task, exit_handler):
"""
Register exit handler for the given task, set it
as self._current_task, and call task.async_start().
as self._current_task, and call task.start().
Subclasses can use this as a generic way to start
a task.
Expand All @@ -112,16 +110,7 @@ def _start_task(self, task, exit_handler):
pass
task.addExitListener(exit_handler)
self._current_task = task
result = asyncio.ensure_future(task.async_start(), loop=self.scheduler)
result.add_done_callback(self._current_task_start_cb)

def _current_task_start_cb(self, future):
try:
future.result()
except asyncio.CancelledError:
self.cancelled = True
self._was_cancelled()
self._async_wait()
task.start()

def _task_queued(self, task):
task.addStartListener(self._task_queued_start_handler)
Expand Down
7 changes: 1 addition & 6 deletions lib/_emerge/TaskSequence.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 1999-2020 Gentoo Authors
# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import sys
Expand Down Expand Up @@ -42,11 +42,6 @@ def _start_next_task(self):

self._start_task(task, self._task_exit_handler)

def _current_task_start_cb(self, future):
CompositeTask._current_task_start_cb(self, future)
if self.cancelled:
self._task_queue.clear()

def _task_exit_handler(self, task):
if self._default_exit(task) != os.EX_OK:
self.wait()
Expand Down
6 changes: 2 additions & 4 deletions lib/portage/tests/ebuild/test_doebuild_fd_pipes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2013-2020 Gentoo Authors
# Copyright 2013-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import portage
Expand All @@ -8,7 +8,6 @@
from portage.package.ebuild._ipc.QueryCommand import QueryCommand
from portage.util._async.ForkProcess import ForkProcess
from portage.util._async.TaskScheduler import TaskScheduler
from portage.util.futures import asyncio
from _emerge.Package import Package
from _emerge.PipeReader import PipeReader

Expand Down Expand Up @@ -55,7 +54,6 @@ def testDoebuild(self):
self.assertEqual(true_binary is None, False,
"true command not found")

loop = asyncio._wrap_loop()
dev_null = open(os.devnull, 'wb')
playground = ResolverPlayground(ebuilds=ebuilds)
try:
Expand Down Expand Up @@ -117,7 +115,7 @@ def testDoebuild(self):
max_jobs=2)

try:
loop.run_until_complete(task_scheduler.async_start())
task_scheduler.start()
finally:
# PipeReader closes pr
os.close(pw)
Expand Down
6 changes: 3 additions & 3 deletions lib/portage/tests/ebuild/test_doebuild_spawn.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2010-2020 Gentoo Authors
# Copyright 2010-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import textwrap
Expand Down Expand Up @@ -92,14 +92,14 @@ def testDoebuildSpawn(self):
ebuild_phase = EbuildPhase(background=False,
phase=phase, scheduler=scheduler,
settings=settings)
scheduler.run_until_complete(ebuild_phase.async_start())
ebuild_phase.start()
ebuild_phase.wait()
self.assertEqual(ebuild_phase.returncode, os.EX_OK)

ebuild_phase = MiscFunctionsProcess(background=False,
commands=['success_hooks'],
scheduler=scheduler, settings=settings)
scheduler.run_until_complete(ebuild_phase.async_start())
ebuild_phase.start()
ebuild_phase.wait()
self.assertEqual(ebuild_phase.returncode, os.EX_OK)

Expand Down
9 changes: 3 additions & 6 deletions lib/portage/tests/ebuild/test_fetch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2020 Gentoo Authors
# Copyright 2019 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

from __future__ import unicode_literals
Expand All @@ -15,7 +15,6 @@
from portage.tests.util.test_socks5 import AsyncHTTPServer
from portage.util.configparser import ConfigParserError
from portage.util.futures import asyncio
from portage.util.futures.compat_coroutine import coroutine, coroutine_return
from portage.util.futures.executor.fork import ForkExecutor
from portage.util._async.SchedulerInterface import SchedulerInterface
from portage.util._eventloop.global_event_loop import global_event_loop
Expand Down Expand Up @@ -194,13 +193,11 @@ def allocate():
def deallocate(settings):
pass

@coroutine
def async_fetch(pkg, ebuild_path):
fetcher = EbuildFetcher(config_pool=config_pool, ebuild_path=ebuild_path,
fetchonly=False, fetchall=True, pkg=pkg, scheduler=loop)
yield fetcher.async_start()
result = yield fetcher.async_wait()
coroutine_return(result)
fetcher.start()
return fetcher.async_wait()

for cpv in ebuilds:
metadata = dict(zip(Package.metadata_keys,
Expand Down
4 changes: 2 additions & 2 deletions lib/portage/tests/ebuild/test_ipc_daemon.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2010-2020 Gentoo Authors
# Copyright 2010-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import tempfile
Expand Down Expand Up @@ -155,7 +155,7 @@ def _run(self, event_loop, task_scheduler, timeout):
task_scheduler.addExitListener(self._exit_callback)

try:
event_loop.run_until_complete(task_scheduler.async_start())
task_scheduler.start()
event_loop.run_until_complete(self._run_done)
event_loop.run_until_complete(task_scheduler.async_wait())
finally:
Expand Down
4 changes: 2 additions & 2 deletions lib/portage/tests/ebuild/test_spawn.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 1998-2020 Gentoo Authors
# Copyright 1998-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import errno
Expand Down Expand Up @@ -34,7 +34,7 @@ def testLogfile(self):
},
scheduler=global_event_loop(),
logfile=logfile)
global_event_loop().run_until_complete(proc.async_start())
proc.start()
os.close(null_fd)
self.assertEqual(proc.wait(), os.EX_OK)
f = io.open(_unicode_encode(logfile,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2010-2020 Gentoo Authors
# Copyright 2010-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import re
Expand Down Expand Up @@ -60,14 +60,14 @@ def testLazyImportPortageBaseline(self):
args=self._baseline_import_cmd,
env=env, fd_pipes={1:slave_fd},
scheduler=scheduler)
scheduler.run_until_complete(producer.async_start())
producer.start()
slave_file.close()

consumer = PipeReader(
input_files={"producer" : master_file},
scheduler=scheduler)

scheduler.run_until_complete(consumer.async_start())
consumer.start()
consumer.wait()
self.assertEqual(producer.wait(), os.EX_OK)
self.assertEqual(consumer.wait(), os.EX_OK)
Expand Down
18 changes: 9 additions & 9 deletions lib/portage/tests/locks/test_asynchronous_lock.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2010-2020 Gentoo Authors
# Copyright 2010-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import signal
Expand Down Expand Up @@ -29,15 +29,15 @@ def _testAsynchronousLock(self):
scheduler=scheduler, _force_async=force_async,
_force_thread=True,
_force_dummy=force_dummy)
scheduler.run_until_complete(async_lock.async_start())
async_lock.start()
self.assertEqual(async_lock.wait(), os.EX_OK)
self.assertEqual(async_lock.returncode, os.EX_OK)
scheduler.run_until_complete(async_lock.async_unlock())

async_lock = AsynchronousLock(path=path,
scheduler=scheduler, _force_async=force_async,
_force_process=True)
scheduler.run_until_complete(async_lock.async_start())
async_lock.start()
self.assertEqual(async_lock.wait(), os.EX_OK)
self.assertEqual(async_lock.returncode, os.EX_OK)
scheduler.run_until_complete(async_lock.async_unlock())
Expand All @@ -63,7 +63,7 @@ def _testAsynchronousLockWait(self):
try:
path = os.path.join(tempdir, 'lock_me')
lock1 = AsynchronousLock(path=path, scheduler=scheduler)
scheduler.run_until_complete(lock1.async_start())
lock1.start()
self.assertEqual(lock1.wait(), os.EX_OK)
self.assertEqual(lock1.returncode, os.EX_OK)

Expand All @@ -73,7 +73,7 @@ def _testAsynchronousLockWait(self):
# one time concurrently.
lock2 = AsynchronousLock(path=path, scheduler=scheduler,
_force_async=True, _force_process=True)
scheduler.run_until_complete(lock2.async_start())
lock2.start()
# lock2 should be waiting for lock1 to release
self.assertEqual(lock2.poll(), None)
self.assertEqual(lock2.returncode, None)
Expand Down Expand Up @@ -104,12 +104,12 @@ def _testAsynchronousLockWaitCancel(self):
try:
path = os.path.join(tempdir, 'lock_me')
lock1 = AsynchronousLock(path=path, scheduler=scheduler)
scheduler.run_until_complete(lock1.async_start())
lock1.start()
self.assertEqual(lock1.wait(), os.EX_OK)
self.assertEqual(lock1.returncode, os.EX_OK)
lock2 = AsynchronousLock(path=path, scheduler=scheduler,
_force_async=True, _force_process=True)
scheduler.run_until_complete(lock2.async_start())
lock2.start()
# lock2 should be waiting for lock1 to release
self.assertEqual(lock2.poll(), None)
self.assertEqual(lock2.returncode, None)
Expand Down Expand Up @@ -142,12 +142,12 @@ def _testAsynchronousLockWaitKill(self):
try:
path = os.path.join(tempdir, 'lock_me')
lock1 = AsynchronousLock(path=path, scheduler=scheduler)
scheduler.run_until_complete(lock1.async_start())
lock1.start()
self.assertEqual(lock1.wait(), os.EX_OK)
self.assertEqual(lock1.returncode, os.EX_OK)
lock2 = AsynchronousLock(path=path, scheduler=scheduler,
_force_async=True, _force_process=True)
scheduler.run_until_complete(lock2.async_start())
lock2.start()
# lock2 should be waiting for lock1 to release
self.assertEqual(lock2.poll(), None)
self.assertEqual(lock2.returncode, None)
Expand Down
6 changes: 3 additions & 3 deletions lib/portage/tests/process/test_PopenProcess.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2012-2020 Gentoo Authors
# Copyright 2012-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import subprocess
Expand Down Expand Up @@ -34,7 +34,7 @@ def _testPipeReader(self, test_string):
consumer = producer.pipe_reader
consumer.input_files = {"producer" : producer.proc.stdout}

global_event_loop().run_until_complete(producer.async_start())
producer.start()
producer.wait()

self.assertEqual(producer.returncode, os.EX_OK)
Expand All @@ -58,7 +58,7 @@ def _testPipeLogger(self, test_string):

producer.pipe_reader = consumer

global_event_loop().run_until_complete(producer.async_start())
producer.start()
producer.wait()

self.assertEqual(producer.returncode, os.EX_OK)
Expand Down
4 changes: 2 additions & 2 deletions lib/portage/tests/process/test_PopenProcessBlockingIO.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2012-2020 Gentoo Authors
# Copyright 2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import subprocess
Expand Down Expand Up @@ -40,7 +40,7 @@ def _testPipeReader(self, test_string):
consumer = producer.pipe_reader
consumer.input_files = {"producer" : producer.proc.stdout}

global_event_loop().run_until_complete(producer.async_start())
producer.start()
producer.wait()

self.assertEqual(producer.returncode, os.EX_OK)
Expand Down
4 changes: 2 additions & 2 deletions lib/portage/tests/process/test_poll.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 1998-2020 Gentoo Authors
# Copyright 1998-2019 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import functools
Expand Down Expand Up @@ -67,7 +67,7 @@ def _testPipeReader(self, master_fd, slave_fd, test_string):
input_files={"producer" : master_file},
_use_array=self._use_array,
scheduler=scheduler)
scheduler.run_until_complete(consumer.async_start())
consumer.start()

producer = scheduler.run_until_complete(asyncio.create_subprocess_exec(
"bash", "-c", self._echo_cmd % test_string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_exit_listener_after_exit(self):
"""
loop = asyncio._wrap_loop()
task = AsynchronousTask(scheduler=loop)
loop.run_until_complete(task.async_start())
task.start()
loop.run_until_complete(task.async_wait())

for i in range(3):
Expand Down

0 comments on commit 71ae5a5

Please sign in to comment.