一. 单进程多线程

1. 使用的模块是Threading。使用join()函数进行阻塞。

 from pdf2txt import pdfTotxt1, pdfTotxt2
import xlrd
import os
from nltk.corpus import PlaintextCorpusReader
from nltk.book import FreqDist
import threading def handleStock(stock_dir, dir, root_txt_path):
for stock in stock_dir:
years_dir=os.listdir(dir+stock)
for y in years_dir:
type_dir=os.listdir(dir+stock+'/'+y)
for t in type_dir:
report_dir=os.listdir(dir+stock+'/'+y+'/'+t)
for r in report_dir:
try:
pdfTotxt1(dir+stock+'/'+y+'/'+t+'/'+r, root_txt_path+stock+'_'+y+'_'+t+'_'+r[:-4]+'.txt')
except:
pdfTotxt2(dir+stock+'/'+y+'/'+t+'/'+r, root_txt_path+stock+'_'+y+'_'+t+'_'+r[:-4]+'.txt') root_pdf_path='/home/luowang/financial_reports_data/attach/'
root_txt_path='/usr/lw/result/txt_reports/'
Threads=[]
if os.path.exists(root_pdf_path):
stock_dir=os.listdir(root_pdf_path)
length=len(stock_dir)
if length>0:
for i in range(0,length, 125):
if i+125 < length:
dir=stock_dir[i:i+125]
obj=threading.Thread(target=handleStock, args=[dir, root_pdf_path, root_txt_path])
Threads.append(obj)
obj.start()
else:
dir=stock_dir[i:length]
obj=threading.Thread(target=handleStock, args=[dir, root_pdf_path, root_txt_path])
Threads.append(obj)
obj.start()

假定主线程中有一些代码,你希望所有下载程序完成后再执行,这时可以调用Thread对象的join()函数将其阻塞,直到该线程完成。如下面代码所示:

for t in Threads:
t.join() print 'all file have been handled !!!'

二. 多进程

1. 使用的模块是subprocess,它常用的函数有Popen(), wait(), poll()

1) wait(): 阻塞自己,等待启动的进程终止。它是Popen对象的方法。

>>> t=subprocess.Popen(['start', 'D:\\github.txt'], shell=True)
>>> t.wait()
0

返回值为0,说明程序正常退出了。

2)poll():检查当前进程是否仍在运行,若是返回None, 若正常结束,则返回0,若不正常结束,则返回非0值。它是Popen对象的方法。

>>> t=subprocess.Popen(['start', 'D:\\github.txt'], shell=True)
>>> t.poll()
0

3)Popen(): 启动计算机中的其他程序。

>>> subprocess.Popen(['start', 'D:\\github.txt'], shell=True)
<subprocess.Popen object at 0x02516210>

这里的‘start’表示使用默认程序打开后面的文件。在Windows上,是start程序;在OS X上是open程序; 在Ubuntu Linux上是see程序。

当然,这里也可以用具体的程序的位置代替,比如下面:

>>> import subprocess
>>> subprocess.Popen('C:\\Windoews\\System32\\notepad.exe', 'D:\\github.txt')

未完待续。。。

python---多线程与多进程的更多相关文章

  1. Python多线程和多进程谁更快?

    python多进程和多线程谁更快 python3.6 threading和multiprocessing 四核+三星250G-850-SSD 自从用多进程和多线程进行编程,一致没搞懂到底谁更快.网上很 ...

  2. python多线程与多进程--存活主机ping扫描以及爬取股票价格

    python多线程与多进程 多线程: 案例:扫描给定网络中存活的主机(通过ping来测试,有响应则说明主机存活) 普通版本: #扫描给定网络中存活的主机(通过ping来测试,有响应则说明主机存活)im ...

  3. Python 多线程、多进程 (三)之 线程进程对比、多进程

    Python 多线程.多进程 (一)之 源码执行流程.GIL Python 多线程.多进程 (二)之 多线程.同步.通信 Python 多线程.多进程 (三)之 线程进程对比.多线程 一.多线程与多进 ...

  4. Python 多线程、多进程 (一)之 源码执行流程、GIL

    Python 多线程.多进程 (一)之 源码执行流程.GIL Python 多线程.多进程 (二)之 多线程.同步.通信 Python 多线程.多进程 (三)之 线程进程对比.多线程 一.python ...

  5. Python 多线程、多进程 (二)之 多线程、同步、通信

    Python 多线程.多进程 (一)之 源码执行流程.GIL Python 多线程.多进程 (二)之 多线程.同步.通信 Python 多线程.多进程 (三)之 线程进程对比.多线程 一.python ...

  6. python多线程与多进程及其区别

    个人一直觉得对学习任何知识而言,概念是相当重要的.掌握了概念和原理,细节可以留给实践去推敲.掌握的关键在于理解,通过具体的实例和实际操作来感性的体会概念和原理可以起到很好的效果.本文通过一些具体的例子 ...

  7. 基于Windows平台的Python多线程及多进程学习小结

    python多线程及多进程对于不同平台有不同的工具(platform-specific tools),如os.fork仅在Unix上可用,而windows不可用,该文仅针对windows平台可用的工具 ...

  8. python 多线程、多进程

    一.首先说下多线程.多进程用途及异同点,另外还涉及到队列的,memcache.redis的操作等: 1.在python中,如果一个程序是IO密集的操作,使用多线程:运算密集的操作使用多进程. 但是,其 ...

  9. python多线程,多进程

    线程是公用内存,进程内存相互独立 python多线程只能是一个cpu,java可以将多个线程平均分配到其他cpu上 以核为单位,所以GIL(全局锁,保证线程安全,数据被安全读取)最小只能控制一个核,很 ...

  10. 搞定python多线程和多进程

    1 概念梳理: 1.1 线程 1.1.1 什么是线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发 ...

随机推荐

  1. 快排+java实现

    import java.util.Arrays; public class QuickSort { //三数取中法.取出不大不小的那个位置 public static int getPivotPos( ...

  2. L2-021 点赞狂魔

    会set的基础用法就可以A了,虽然是L2题,但是不难,代码如下,很好理解,set在这里不解释了自己去网上查一下就明白了: #include<stdio.h> #include<str ...

  3. FileNotFoundException报错, src\main\....\....(拒绝访问) , 原因:1. 方法没有判断文件夹和文件, 2.没有指明文件的具体路径和名字

  4. 学习笔记49—matlab FDR校正

    matlab自带函数mafdr,当ttest数较多时,可直接用[FDR, Q]=mafdr(P):但是Storey procedure在p值少于1000个时会崩溃,此时应改用BH FDR方法:mafd ...

  5. angular在组件中选择dom元素

    想选择 在组件中选择自己template里的dom元素,要使用ElementRef.     import { Component, EventEmitter, HostListener, OnIni ...

  6. ffmpeg 加 logo

    How to add a watermark or logo to any corner or the center of a video with FFMPEG. ffmpeg –i video.m ...

  7. change color

    关于DataGridView行和列的背景色-前景色设置 1.设定DataGridView全部单元格的Style   DataGridView内所有单元格的Style变更,可以使用DataGridVie ...

  8. English trip V1 - 11.What's That? 那是什么?Teacher:Patrick Key:There's/There are

    In this lesson you will learn to describe the position of an object. 在本课中,您将学习如何描述对象的位置. 课上内容(Lesson ...

  9. 【IOS学习】【Swift语言】

    基本语法: OS X playground 引入 import Cocoa IOS playground 引入 import UIKit 基本数据类型 let 定义常量 定义完成之后无法修改 var ...

  10. A Chess Game POJ - 2425

    Let's design a new chess game. There are N positions to hold M chesses in this game. Multiple chesse ...