Skip to content

Update p12_using_generators_as_alternative_to_threads.rst #277

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

Merged
merged 1 commit into from
Apr 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions source/c12/p12_using_generators_as_alternative_to_threads.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
def new_task(self, task):
'''
Admit a newly started task to the scheduler

'''
self._task_queue.append(task)

Expand Down Expand Up @@ -89,7 +88,7 @@
...

到此为止,我们实际上已经实现了一个“操作系统”的最小核心部分。
生成器函数就是认为,而yield语句是任务挂起的信号。
生成器函数就是任务,而yield语句是任务挂起的信号。
调度器循环检查任务列表直到没有任务要执行为止。

实际上,你可能想要使用生成器来实现简单的并发。
Expand All @@ -103,7 +102,7 @@

class ActorScheduler:
def __init__(self):
self._actors = { } # Mapping of names to actors
self._actors = {} # Mapping of names to actors
self._msg_queue = deque() # Message queue

def new_actor(self, name, actor):
Expand Down Expand Up @@ -148,7 +147,6 @@
# Send to the printer task
sched.send('printer', n)
# Send the next count to the counter task (recursive)

sched.send('counter', n-1)

sched = ActorScheduler()
Expand All @@ -175,6 +173,7 @@
class YieldEvent:
def handle_yield(self, sched, task):
pass

def handle_resume(self, sched, task):
pass

Expand All @@ -201,7 +200,6 @@
'''
Add a newly started task to the scheduler
'''

self._ready.append((task, None))
self._numtasks += 1

Expand Down Expand Up @@ -253,18 +251,21 @@
def __init__(self, sock, data):
self.sock = sock
self.data = data

def handle_yield(self, sched, task):

sched._write_wait(self.sock.fileno(), self, task)

def handle_resume(self, sched, task):
nsent = self.sock.send(self.data)
sched.add_ready(task, nsent)

class AcceptSocket(YieldEvent):
def __init__(self, sock):
self.sock = sock

def handle_yield(self, sched, task):
sched._read_wait(self.sock.fileno(), self, task)

def handle_resume(self, sched, task):
r = self.sock.accept()
sched.add_ready(task, r)
Expand All @@ -273,12 +274,16 @@
class Socket(object):
def __init__(self, sock):
self._sock = sock

def recv(self, maxbytes):
return ReadSocket(self._sock, maxbytes)

def send(self, data):
return WriteSocket(self._sock, data)

def accept(self):
return AcceptSocket(self._sock)

def __getattr__(self, name):
return getattr(self._sock, name)

Expand Down