-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
175 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,12 @@ | |
|
||
* 中文繁体版PDF下载地址: http://pan.baidu.com/s/1eRjFDaM | ||
|
||
------------------------------------------------------------- | ||
|
||
如果您认为本书读后收获很大,不妨小额赞助我一下,让我有动力继续翻译高质量的教程。^_^ | ||
|
||
支付宝账号:[email protected] | ||
|
||
-------------------------------------------------------------- | ||
|
||
++++++++++++++++ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env python | ||
# -*- encoding: utf-8 -*- | ||
""" | ||
Topic: 素数生成 | ||
Desc : 埃氏筛法算法 | ||
""" | ||
|
||
|
||
def _odd_iter(): | ||
'''构造以3开始的奇数序列''' | ||
n = 1 | ||
while True: | ||
n += 2 | ||
yield n | ||
|
||
|
||
def _not_divisible(n): | ||
return lambda x: x % n > 0 | ||
|
||
|
||
def primes(): | ||
yield 2 | ||
|
||
it = _odd_iter() | ||
while True: | ||
n = next(it) | ||
yield n | ||
it = filter(_not_divisible(n), it) | ||
|
||
for n in primes(): | ||
if n < 1000: | ||
print(n, end=' ') | ||
else: | ||
break | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env python | ||
# -*- encoding: utf-8 -*- | ||
""" | ||
Topic: 多线程示例程序 | ||
Desc : | ||
""" | ||
|
||
from multiprocessing import Process | ||
import os | ||
|
||
# 子进程要执行的代码 | ||
def run_proc(name): | ||
print('Run child process %s (%s)...' % (name, os.getpid())) | ||
|
||
if __name__=='__main__': | ||
print('Parent process %s.' % os.getpid()) | ||
p = Process(target=run_proc, args=('test',)) | ||
print('Child process will start.') | ||
p.start() | ||
p.join() | ||
print('Child process end.') | ||
|
||
|
||
from multiprocessing import Pool | ||
import os, time, random | ||
|
||
def long_time_task(name): | ||
print('Run task %s (%s)...' % (name, os.getpid())) | ||
start = time.time() | ||
time.sleep(random.random() * 3) | ||
end = time.time() | ||
print('Task %s runs %0.2f seconds.' % (name, (end - start))) | ||
|
||
if __name__=='__main__': | ||
print('Parent process %s.' % os.getpid()) | ||
p = Pool(4) | ||
for i in range(5): | ||
p.apply_async(long_time_task, args=(i,)) | ||
print('Waiting for all subprocesses done...') | ||
p.close() | ||
p.join() | ||
print('All subprocesses done.') | ||
|
||
|
||
from multiprocessing import Process, Queue | ||
import os, time, random | ||
|
||
# 写数据进程执行的代码: | ||
def write(q): | ||
print('Process to write: %s' % os.getpid()) | ||
for value in ['A', 'B', 'C']: | ||
print('Put %s to queue...' % value) | ||
q.put(value) | ||
time.sleep(random.random()) | ||
|
||
# 读数据进程执行的代码: | ||
def read(q): | ||
print('Process to read: %s' % os.getpid()) | ||
while True: | ||
value = q.get(True) | ||
print('Get %s from queue.' % value) | ||
|
||
if __name__=='__main__': | ||
# 父进程创建Queue,并传给各个子进程: | ||
q = Queue() | ||
pw = Process(target=write, args=(q,)) | ||
pr = Process(target=read, args=(q,)) | ||
# 启动子进程pw,写入: | ||
pw.start() | ||
# 启动子进程pr,读取: | ||
pr.start() | ||
# 等待pw结束: | ||
pw.join() | ||
# pr进程里是死循环,无法等待其结束,只能强行终止: | ||
pr.terminate() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
*关于译者* | ||
|
||
* 姓名: 熊能 | ||
* 微信: yidao620 | ||
* Email: [email protected] | ||
* 博客: http://yidao620c.github.io/ | ||
* GitHub: https://github.com/yidao620c | ||
|
@@ -17,3 +18,10 @@ | |
https://github.com/yidao620c/python3-cookbook | ||
|
||
| | ||
*友情赞助* | ||
|
||
如果您认为本书读后收获很大,不妨小额赞助我一下,让我有动力继续翻译高质量的教程。^_^ | ||
|
||
支付宝账号:[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters