python 多线程串行和并行的实例

如下所示:

#coding=utf-8
import threading
import time
import cx_Oracle
from pprint import pprint
import csv
table_name = "dbtest.csv"
f = open(table_name + ".csv", "w")

def exp01():
  conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
  cursor = conn.cursor()
  owner = "system"
  writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
  tname = threading.current_thread()
  print tname
  exportOracleSql = "select 'exp01' from dual"
  print exportOracleSql
  x = cursor.execute(exportOracleSql)
  time.sleep(1000)
  cursor.close()
  conn.close()

def exp02():
  conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
  cursor = conn.cursor()
  owner = "system"
  writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
  tname = threading.current_thread()
  print tname
  exportOracleSql = "select 'exp02' from dual"
  print exportOracleSql
  x = cursor.execute(exportOracleSql)
  time.sleep(1000)
  cursor.close()
  conn.close()

def exp03():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp03' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp04():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp04' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp05():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp05' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp06():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp06' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp07():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp07' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp08():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp08' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp09():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp09' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp10():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp10' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp11():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp11' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp12():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp12' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp13():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp13' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp14():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp14' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

threads = []
t1 = threading.Thread(target=exp01,name='exp01')
threads.append(t1)
t2 = threading.Thread(target=exp02,name='exp02')
threads.append(t2)
t2 = threading.Thread(target=exp03,name='exp03')
threads.append(t2)
t2 = threading.Thread(target=exp04,name='exp04')
threads.append(t2)
t2 = threading.Thread(target=exp05,name='exp05')
threads.append(t2)
t2 = threading.Thread(target=exp06,name='exp06')
threads.append(t2)
t2 = threading.Thread(target=exp07,name='exp07')
threads.append(t2)
t2 = threading.Thread(target=exp08,name='exp08')
threads.append(t2)
t2 = threading.Thread(target=exp09,name='exp09')
threads.append(t2)
t2 = threading.Thread(target=exp10,name='exp10')
threads.append(t2)
t2 = threading.Thread(target=exp11,name='exp11')
threads.append(t2)
t2 = threading.Thread(target=exp12,name='exp12')
threads.append(t2)
t2 = threading.Thread(target=exp13,name='exp13')
threads.append(t2)
t2 = threading.Thread(target=exp14,name='exp14')
threads.append(t2)

if __name__ == '__main__':
  for t in threads:
    #t.setDaemon(True)
    t.start()
    #t.run()
    #t.start()
    # print '3333333'
    print threading.current_thread()
    # print t.is_alive()
    # print '3333333'
  t.join()
  print "all over "

输出:
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p7.py
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<_MainThread(MainThread, started 156528)>
<Thread(exp01, started 155004)>
select 'exp01' from dual
<Thread(exp12, started 155744)>
 select 'exp12' from dual<Thread(exp02, started 155000)>

select 'exp02' from dual
<Thread(exp08, started 155728)>
select 'exp08' from dual
<Thread(exp06, started 155752)>
select 'exp06' from dual
<Thread(exp03, started 154816)>
select 'exp03' from dual
<Thread(exp09, started 156544)>
select 'exp09' from dual
<Thread(exp11, started 155760)>
select 'exp11' from dual
<Thread(exp04, started 154112)>
select 'exp04' from dual
<Thread(exp10, started 155764)><Thread(exp05, started 154640)>

select 'exp10' from dualselect 'exp05' from dual

<Thread(exp07, started 155188)>
select 'exp07' from dual
<Thread(exp13, started 154600)>
select 'exp13' from dual
<Thread(exp14, started 155804)>
select 'exp14' from dual

  	SID	SERIAL#	USERNAME	PREV_SQL_ID
1	9	1179	TEST	01tjnxmmurdw7
2	10	75	TEST	g5ph474nsjvwv
3	12	907	TEST	87mdhpgj9k5tz
4	419	1303	TEST	4g8r4bkf8aq3n
5	420	655	TEST	1rx9mjdvp1udx
6	421	1955	TEST	928r7khrtn4jd
7	424	51	TEST	d6sgjjwpm74qz
8	839	3	TEST	a1hg7hrwgrdqk
9	840	43	TEST	fqjuj4qp5hmf0
10	841	111	TEST	2jzx0889h3k5n
11	1252	145	TEST	awtzfbx7dhn88
12	1253	7	TEST	55tzs9gdmcd4p
13	1254	9	TEST	4v01fvb5sj7k4
14	1255	265	TEST	39bcsfjr5y62b

此时是并发执行

改变join的位置:
#coding=utf-8
import threading
import time
import cx_Oracle
from pprint import pprint
import csv
table_name = "dbtest.csv"
f = open(table_name + ".csv", "w")

def exp01():
  conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
  cursor = conn.cursor()
  owner = "system"
  writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
  tname = threading.current_thread()
  print tname
  exportOracleSql = "select 'exp01' from dual"
  print exportOracleSql
  x = cursor.execute(exportOracleSql)
  time.sleep(1000)
  cursor.close()
  conn.close()

def exp02():
  conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
  cursor = conn.cursor()
  owner = "system"
  writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
  tname = threading.current_thread()
  print tname
  exportOracleSql = "select 'exp02' from dual"
  print exportOracleSql
  x = cursor.execute(exportOracleSql)
  time.sleep(1000)
  cursor.close()
  conn.close()

def exp03():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp03' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp04():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp04' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp05():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp05' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp06():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp06' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp07():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp07' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp08():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp08' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp09():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp09' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp10():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp10' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp11():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp11' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp12():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp12' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp13():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp13' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp14():
    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
    cursor = conn.cursor()
    owner = "system"
    writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp14' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

threads = []
t1 = threading.Thread(target=exp01,name='exp01')
threads.append(t1)
t2 = threading.Thread(target=exp02,name='exp02')
threads.append(t2)
t2 = threading.Thread(target=exp03,name='exp03')
threads.append(t2)
t2 = threading.Thread(target=exp04,name='exp04')
threads.append(t2)
t2 = threading.Thread(target=exp05,name='exp05')
threads.append(t2)
t2 = threading.Thread(target=exp06,name='exp06')
threads.append(t2)
t2 = threading.Thread(target=exp07,name='exp07')
threads.append(t2)
t2 = threading.Thread(target=exp08,name='exp08')
threads.append(t2)
t2 = threading.Thread(target=exp09,name='exp09')
threads.append(t2)
t2 = threading.Thread(target=exp10,name='exp10')
threads.append(t2)
t2 = threading.Thread(target=exp11,name='exp11')
threads.append(t2)
t2 = threading.Thread(target=exp12,name='exp12')
threads.append(t2)
t2 = threading.Thread(target=exp13,name='exp13')
threads.append(t2)
t2 = threading.Thread(target=exp14,name='exp14')
threads.append(t2)

if __name__ == '__main__':
  for t in threads:
    #t.setDaemon(True)
    t.start()
    #t.run()
    #t.start()
    # print '3333333'
    print threading.current_thread()
    # print t.is_alive()
    # print '3333333'
    t.join()
  print "all over "

C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p7.py
<_MainThread(MainThread, started 154776)>
<Thread(exp01, started 156240)>
select 'exp01' from dual

此时变成串行,数据库连接也只有一个:

  	SID	SERIAL#	USERNAME	PREV_SQL_ID
1	421	1957	TEST	928r7khrtn4jd

以上这篇python 多线程串行和并行的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 详解python多线程之间的同步(一)

    引言: 线程之间经常需要协同工作,通过某种技术,让一个线程访问某些数据时,其它线程不能访问这些数据,直到该线程完成对数据的操作.这些技术包括临界区(Critical Section),互斥量(Mutex),信号量(Semaphore),事件Event等. Event threading库中的event对象通过使用内部一个flag标记,通过flag的True或者False的变化来进行操作.      名称                                      含义 set( )

  • Python多线程同步---文件读写控制方法

    1.实现文件读写的文件ltz_schedule_times.py #! /usr/bin/env python #coding=utf-8 import os def ReadTimes(): res = [] if os.path.exists('schedule_times.txt'): fp = open('schedule_times.txt', 'r') else: os.system('touch schedule_times.txt') fp = open('schedule_ti

  • python批量爬取下载抖音视频

    本文实例为大家分享了python批量爬取下载抖音视频的具体代码,供大家参考,具体内容如下 import os import requests import re import sys import asyncio import aiohttp headers = { 'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) ' 'Ve

  • python爬虫爬取微博评论案例详解

    前几天,杨超越编程大赛火了,大家都在报名参加,而我也是其中的一员. 在我们的项目中,我负责的是数据爬取这块,我主要是把对于杨超越 的每一条评论的相关信息. 数据格式:{"name":评论人姓名,"comment_time":评论时间,"comment_info":评论内容,"comment_url":评论人的主页} 以上就是我们需要的信息. 爬虫前的分析: 以上是杨超越的微博主页,这是我们首先需要获取到的内容. 因为我们需要等

  • python多线程+代理池爬取天天基金网、股票数据过程解析

    简介 提到爬虫,大部分人都会想到使用Scrapy工具,但是仅仅停留在会使用的阶段.为了增加对爬虫机制的理解,我们可以手动实现多线程的爬虫过程,同时,引入IP代理池进行基本的反爬操作. 本次使用天天基金网进行爬虫,该网站具有反爬机制,同时数量足够大,多线程效果较为明显. 技术路线 IP代理池 多线程 爬虫与反爬 编写思路 首先,开始分析天天基金网的一些数据.经过抓包分析,可知: ./fundcode_search.js包含所有基金的数据,同时,该地址具有反爬机制,多次访问将会失败的情况. 同时,经

  • python3 requests中使用ip代理池随机生成ip的实例

    啥也不说了,直接上代码吧! # encoding:utf-8 import requests # 导入requests模块用于访问测试自己的ip import random pro = ['1.119.129.2:8080', '115.174.66.148', '113.200.214.164'] # 在(http://www.xicidaili.com/wt/)上面收集的ip用于测试 # 没有使用字典的原因是 因为字典中的键是唯一的 http 和https 只能存在一个 所以不建议使用字典

  • 对Python多线程读写文件加锁的实例详解

    Python的多线程在io方面比单线程还是有优势,但是在多线程开发时,少不了对文件的读写操作.在管理多个线程对同一文件的读写操作时,就少不了文件锁了. 使用fcntl 在linux下,python的标准库有现成的文件锁,来自于fcntl模块.这个模块提供了unix系统fcntl()和ioctl()的接口. 对于文件锁的操作,主要需要使用 fcntl.flock(fd, operation)这个函数. 其中,参数 fd 表示文件描述符:参数 operation 指定要进行的锁操作,该参数的取值有如

  • python 多线程串行和并行的实例

    如下所示: #coding=utf-8 import threading import time import cx_Oracle from pprint import pprint import csv table_name = "dbtest.csv" f = open(table_name + ".csv", "w") def exp01(): conn = cx_Oracle.connect('test/test@192.168.137.

  • Python多线程threading和multiprocessing模块实例解析

    本文研究的主要是Python多线程threading和multiprocessing模块的相关内容,具体介绍如下. 线程是一个进程的实体,是由表示程序运行状态的寄存器(如程序计数器.栈指针)以及堆栈组成,它是比进程更小的单位. 线程是程序中的一个执行流.一个执行流是由CPU运行程序代码并操作程序的数据所形成的.因此,线程被认为是以CPU为主体的行为. 线程不包含进程地址空间中的代码和数据,线程是计算过程在某一时刻的状态.所以,系统在产生一个线程或各个线程之间切换时,负担要比进程小得多. 线程是一

  • Python多线程获取返回值代码实例

    这篇文章主要介绍了Python多线程获取返回值代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在使用多线程的时候难免想要获取其操作完的返回值进行其他操作,下面的方法以作参考: 一,首先重写threading类,使其满足调用特定的方法获取其返回值 import threading class MyThread(threading.Thread): """重写多线程,使其能够返回值""" d

  • JavaScript异步操作中串行和并行

    目录 1.前言 2.es5方式 3.异步函数串行执行 4.异步函数并行执行 5.异步函数串行执行和并行执行结合 6.es6方式 7.async 和await 结合promise all 1.前言 本文写一下js中es5和es6针对异步函数,串行执行和并行执行的方案.已经串行和并行结合使用的例子. 2.es5方式 在es6出来之前,社区nodejs中针对回调地狱,已经有了promise方案.假如多个异步函数,执行循序怎么安排,如何才能更快的执行完所有异步函数,再执行下一步呢?这里就出现了js的串行

  • Python多线程及其基本使用方法实例分析

    本文实例讲述了Python多线程及其基本使用方法.分享给大家供大家参考,具体如下: 学习Python的多线程(Multi-threading),至少应该要有进程与线程的基本概念,可以参考:http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html. 1.Python中的多线程 执行一个程序,即在操作系统中开启了一个进程,在某一时刻,一个CPU内核只能进行一个进程的任务,现在的计算机所说的多进程/多任务其实是通过加快CPU的执

  • python多线程threading.Lock锁用法实例

    本文实例讲述了python多线程threading.Lock锁的用法实例,分享给大家供大家参考.具体分析如下: python的锁可以独立提取出来 复制代码 代码如下: mutex = threading.Lock() #锁的使用 #创建锁 mutex = threading.Lock() #锁定 mutex.acquire([timeout]) #释放 mutex.release() 锁定方法acquire可以有一个超时时间的可选参数timeout.如果设定了timeout,则在超时后通过返回值

  • Python多线程通信queue队列用法实例分析

    本文实例讲述了Python多线程通信queue队列用法.分享给大家供大家参考,具体如下: queue: 什么是队列:是一种特殊的结构,类似于列表.不过就像排队一样,队列中的元素一旦取出,那么就会从队列中删除. 线程之间的通信可以使用队列queue来进行 线程如何使用queue.Queue[还有其他类型的对象下面讲]来通信: 1.创建一个Queue对象:对象=queue.Queue(x),x是队列容量,x可以不填,默认没有容量限制, 2.get()可以使线程从队列中获取一个元素,如果队列为空,ge

  • Python多线程thread及模块使用实例

    多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进度条来显示处理的进度 程序的运行速度可能加快 在一些等待的任务实现上如用户输入.文件读写和网络收发数据等,线程就比较有用了.在这种情况下我们可以释放一些珍贵的资源如内存占用等等. 线程在执行过程中与进程还是有区别的.每个独立的线程有一个程序运行的入口.顺序执行序列和程序的出口.但是线程不能够独立执行

  • Python多线程中阻塞(join)与锁(Lock)使用误区解析

    关于阻塞主线程 join的错误用法 Thread.join() 作用为阻塞主线程,即在子线程未返回的时候,主线程等待其返回然后再继续执行. join不能与start在循环里连用 以下为错误代码,代码创建了5个线程,然后用一个循环激活线程,激活之后令其阻塞主线程. threads = [Thread() for i in range(5)] for thread in threads: thread.start() thread.join() 执行过程: 1. 第一次循环中,主线程通过start函

随机推荐