python 串行执行和并行执行实例

我就废话不多说了,大家还是直接看代码吧!

#coding=utf-8

import threading

import time

import cx_Oracle

from pprint import pprint

import csv

print time.asctime()

table_name = "dbtest.csv"

f = open(table_name + ".csv", "w")

conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')

cursor = conn.cursor()

def query01():

  tname = threading.current_thread()

  aa=10

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

def query02():

  tname = threading.current_thread()

  aa=20

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

def query03():

  tname = threading.current_thread()

  aa=30

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

def query04():

  tname = threading.current_thread()

  aa=40

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

def query05():

  tname = threading.current_thread()

  aa=50

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

def query06():

  tname = threading.current_thread()

  aa=60

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

def query07():

  tname = threading.current_thread()

  aa=70

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  return row

  cursor.close()

def query08():

  tname = threading.current_thread()

  aa=80

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

def query09():

  tname = threading.current_thread()

  aa=90

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

def query10():

  tname = threading.current_thread()

  aa=100

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close() 

threads = []

t1 = threading.Thread(target=query01, name='query01')

threads.append(t1)

t2 = threading.Thread(target=query02, name='query02')

threads.append(t2)

t2 = threading.Thread(target=query03, name='query03')

threads.append(t2)

t2 = threading.Thread(target=query04, name='query04')

threads.append(t2)

t2 = threading.Thread(target=query05, name='query05')

threads.append(t2)

t2 = threading.Thread(target=query06, name='query06')

threads.append(t2)

t2 = threading.Thread(target=query07, name='query07')

threads.append(t2)

t2 = threading.Thread(target=query08, name='query08')

threads.append(t2)

t2 = threading.Thread(target=query09, name='query09')

threads.append(t2)

t2 = threading.Thread(target=query10, name='query10')

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 "

  print time.asctime() 

C:\Python27\python.exe C:/Users/tlcb/PycharmProjects/untitled/a2.py

Wed Mar 28 11:08:19 2018

<_MainThread(MainThread, started 18744)>

[(10, '10boobook10', '10aaaaaaaaaaaa10', '10bbbbbbbbbbbbbbbbb10'), (10, '10sssssssss10', 'tlcb', 'tlcb'), (10, '10boobook10', '10aaaaaaaaaaaa10', '10bbbbbbbbbbbbbbbbb10')]

<_MainThread(MainThread, started 18744)>

[(20, '20boobook20', '20aaaaaaaaaaaa20', '20bbbbbbbbbbbbbbbbb20'), (20, '20boobook20', '20aaaaaaaaaaaa20', '20bbbbbbbbbbbbbbbbb20'), (20, '20boobook20', '20aaaaaaaaaaaa20', '20bbbbbbbbbbbbbbbbb20')]

<_MainThread(MainThread, started 18744)>

[(30, '30boobook30', '30aaaaaaaaaaaa30', '30bbbbbbbbbbbbbbbbb30'), (30, '30boobook30', '30aaaaaaaaaaaa30', '30bbbbbbbbbbbbbbbbb30'), (30, '30boobook30', '30aaaaaaaaaaaa30', '30bbbbbbbbbbbbbbbbb30')]

<_MainThread(MainThread, started 18744)>

[(40, '40boobook40', '40aaaaaaaaaaaa40', '40bbbbbbbbbbbbbbbbb40'), (40, '40boobook40', '40aaaaaaaaaaaa40', '40bbbbbbbbbbbbbbbbb40'), (40, '40boobook40', '40aaaaaaaaaaaa40', '40bbbbbbbbbbbbbbbbb40')]

<_MainThread(MainThread, started 18744)>

[(50, '50boobook50', '50aaaaaaaaaaaa50', '50bbbbbbbbbbbbbbbbb50'), (50, '50boobook50', '50aaaaaaaaaaaa50', '50bbbbbbbbbbbbbbbbb50'), (50, '50boobook50', '50aaaaaaaaaaaa50', '50bbbbbbbbbbbbbbbbb50')]

<_MainThread(MainThread, started 18744)>

[(60, '60boobook60', '60aaaaaaaaaaaa60', '60bbbbbbbbbbbbbbbbb60'), (60, '60boobook60', '60aaaaaaaaaaaa60', '60bbbbbbbbbbbbbbbbb60'), (60, '60boobook60', '60aaaaaaaaaaaa60', '60bbbbbbbbbbbbbbbbb60')]

<_MainThread(MainThread, started 18744)>

<_MainThread(MainThread, started 18744)>

[(80, '80boobook80', '80aaaaaaaaaaaa80', '80bbbbbbbbbbbbbbbbb80'), (80, '80boobook80', '80aaaaaaaaaaaa80', '80bbbbbbbbbbbbbbbbb80'), (80, '80boobook80', '80aaaaaaaaaaaa80', '80bbbbbbbbbbbbbbbbb80')]

<_MainThread(MainThread, started 18744)>

[(90, '90boobook90', '90aaaaaaaaaaaa90', '90bbbbbbbbbbbbbbbbb90'), (90, '90boobook90', '90aaaaaaaaaaaa90', '90bbbbbbbbbbbbbbbbb90'), (90, '90boobook90', '90aaaaaaaaaaaa90', '90bbbbbbbbbbbbbbbbb90')]

<_MainThread(MainThread, started 18744)>

[(100, '100boobook100', '100aaaaaaaaaaaa100', '100bbbbbbbbbbbbbbbbb100'), (100, '100boobook100', '100aaaaaaaaaaaa100', '100bbbbbbbbbbbbbbbbb100'), (100, '100boobook100', '100aaaaaaaaaaaa100', '100bbbbbbbbbbbbbbbbb100')]

all over 

Wed Mar 28 11:08:34 2018 

Process finished with exit code 0 

这个时候是串行 花费了15秒 

多线程跑:

#coding=utf-8

import threading

import time

import cx_Oracle

from pprint import pprint

import csv

print time.asctime()

table_name = "dbtest.csv"

f = open(table_name + ".csv", "w") 

def query01():

  tname = threading.current_thread()

  aa=10

  conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')

  cursor = conn.cursor()

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

  conn.close()

def query02():

  tname = threading.current_thread()

  aa=20

  conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')

  cursor = conn.cursor()

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

  conn.close()

def query03():

  tname = threading.current_thread()

  aa=30

  conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')

  cursor = conn.cursor()

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

  conn.close()

def query04():

  conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')

  cursor = conn.cursor()

  tname = threading.current_thread()

  aa=40

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

  conn.close()

def query05():

  conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')

  cursor = conn.cursor()

  tname = threading.current_thread()

  aa=50

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

  conn.close()

def query06():

  conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')

  cursor = conn.cursor()

  tname = threading.current_thread()

  aa=60

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

  conn.close()

def query07():

  conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')

  cursor = conn.cursor()

  tname = threading.current_thread()

  aa=70

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  return row

  cursor.close()

  conn.close()

def query08():

  conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')

  cursor = conn.cursor()

  tname = threading.current_thread()

  aa=80

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

  conn.close()

def query09():

  conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')

  cursor = conn.cursor()

  tname = threading.current_thread()

  aa=90

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

  conn.close()

def query10():

  conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')

  cursor = conn.cursor()

  tname = threading.current_thread()

  aa=100

  # cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])

  cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")

  cursor.execute(None,{'id':aa})

  row = cursor.fetchall()

  print row

  return row

  cursor.close()

  conn.close() 

threads = []

t1 = threading.Thread(target=query01, name='query01')

threads.append(t1)

t2 = threading.Thread(target=query02, name='query02')

threads.append(t2)

t2 = threading.Thread(target=query03, name='query03')

threads.append(t2)

t2 = threading.Thread(target=query04, name='query04')

threads.append(t2)

t2 = threading.Thread(target=query05, name='query05')

threads.append(t2)

t2 = threading.Thread(target=query06, name='query06')

threads.append(t2)

t2 = threading.Thread(target=query07, name='query07')

threads.append(t2)

t2 = threading.Thread(target=query08, name='query08')

threads.append(t2)

t2 = threading.Thread(target=query09, name='query09')

threads.append(t2)

t2 = threading.Thread(target=query10, name='query10')

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 "

  print time.asctime() 

C:\Python27\python.exe C:/Users/tlcb/PycharmProjects/untitled/a2.py

Wed Mar 28 11:12:47 2018

<_MainThread(MainThread, started 22500)>

<_MainThread(MainThread, started 22500)>

<_MainThread(MainThread, started 22500)>

<_MainThread(MainThread, started 22500)>

<_MainThread(MainThread, started 22500)>

<_MainThread(MainThread, started 22500)>

<_MainThread(MainThread, started 22500)>

<_MainThread(MainThread, started 22500)>

<_MainThread(MainThread, started 22500)>

<_MainThread(MainThread, started 22500)>

[(40, '40boobook40', '40aaaaaaaaaaaa40', '40bbbbbbbbbbbbbbbbb40'), (40, '40boobook40', '40aaaaaaaaaaaa40', '40bbbbbbbbbbbbbbbbb40'), (40, '40boobook40', '40aaaaaaaaaaaa40', '40bbbbbbbbbbbbbbbbb40')]

[(60, '60boobook60', '60aaaaaaaaaaaa60', '60bbbbbbbbbbbbbbbbb60'), (60, '60boobook60', '60aaaaaaaaaaaa60', '60bbbbbbbbbbbbbbbbb60'), (60, '60boobook60', '60aaaaaaaaaaaa60', '60bbbbbbbbbbbbbbbbb60')]

[(80, '80boobook80', '80aaaaaaaaaaaa80', '80bbbbbbbbbbbbbbbbb80'), (80, '80boobook80', '80aaaaaaaaaaaa80', '80bbbbbbbbbbbbbbbbb80'), (80, '80boobook80', '80aaaaaaaaaaaa80', '80bbbbbbbbbbbbbbbbb80')]

[(50, '50boobook50', '50aaaaaaaaaaaa50', '50bbbbbbbbbbbbbbbbb50'), (50, '50boobook50', '50aaaaaaaaaaaa50', '50bbbbbbbbbbbbbbbbb50'), (50, '50boobook50', '50aaaaaaaaaaaa50', '50bbbbbbbbbbbbbbbbb50')]

[(10, '10boobook10', '10aaaaaaaaaaaa10', '10bbbbbbbbbbbbbbbbb10'), (10, '10sssssssss10', 'tlcb', 'tlcb'), (10, '10boobook10', '10aaaaaaaaaaaa10', '10bbbbbbbbbbbbbbbbb10')]

[(20, '20boobook20', '20aaaaaaaaaaaa20', '20bbbbbbbbbbbbbbbbb20'), (20, '20boobook20', '20aaaaaaaaaaaa20', '20bbbbbbbbbbbbbbbbb20'), (20, '20boobook20', '20aaaaaaaaaaaa20', '20bbbbbbbbbbbbbbbbb20')]

[(30, '30boobook30', '30aaaaaaaaaaaa30', '30bbbbbbbbbbbbbbbbb30'), (30, '30boobook30', '30aaaaaaaaaaaa30', '30bbbbbbbbbbbbbbbbb30'), (30, '30boobook30', '30aaaaaaaaaaaa30', '30bbbbbbbbbbbbbbbbb30')]

[(100, '100boobook100', '100aaaaaaaaaaaa100', '100bbbbbbbbbbbbbbbbb100'), (100, '100boobook100', '100aaaaaaaaaaaa100', '100bbbbbbbbbbbbbbbbb100'), (100, '100boobook100', '100aaaaaaaaaaaa100', '100bbbbbbbbbbbbbbbbb100')]

[(90, '90boobook90', '90aaaaaaaaaaaa90', '90bbbbbbbbbbbbbbbbb90'), (90, '90boobook90', '90aaaaaaaaaaaa90', '90bbbbbbbbbbbbbbbbb90'), (90, '90boobook90', '90aaaaaaaaaaaa90', '90bbbbbbbbbbbbbbbbb90')]

all over 

Wed Mar 28 11:12:55 2018 

Process finished with exit code 0 

此时花了 8秒

补充知识:python logging定制logstash的json日志格式

最近一直在折腾日志的收集,现在算是收尾了。 写一篇算python优化logstash的方案。

其实大家都知道logstash调用grok来解析日志的话,是要消耗cpu的成本的,毕竟是需要正则的匹配的。

根据logstash调优的方案,咱们可以预先生成json的格式。 我这边基本是python的程序,怎么搞尼 ?

有两种方法,第一种方法是生成json后,直接打入logstash的端口。 还有一种是生成json写入文件,让logstash做tail操作的时候,把一行的日志数据直接载入json就可以了。

python下的日志调试用得时logging,改成json也是很好改得。 另外不少老外已经考虑到这样的需求,已经做了python logstash的模块。

import logging
import logstash
import sys

host = 'localhost'

test_logger = logging.getLogger('python-logstash-logger')
test_logger.setLevel(logging.INFO)
test_logger.addHandler(logstash.LogstashHandler(host, 5959, version=1))
# test_logger.addHandler(logstash.TCPLogstashHandler(host, 5959, version=1))

test_logger.error('python-logstash: test logstash error message.')
test_logger.info('python-logstash: test logstash info message.')
test_logger.warning('python-logstash: test logstash warning message.')

# add extra field to logstash message
extra = {
  'test_string': 'python version: ' + repr(sys.version_info),
  'test_boolean': True,
  'test_dict': {'a': 1, 'b': 'c'},
  'test_float': 1.23,
  'test_integer': 123,
  'test_list': [1, 2, '3'],
}
test_logger.info('python-logstash: test extra fields', extra=extra)

python-logstash自带了amqp的方案

import logging
import logstash

# AMQP parameters
host = 'localhost'
username = 'guest'
password= 'guest'
exchange = 'logstash.py'

# get a logger and set logging level
test_logger = logging.getLogger('python-logstash-logger')
test_logger.setLevel(logging.INFO)

# add the handler
test_logger.addHandler(logstash.AMQPLogstashHandler(version=1,
                          host=host,
                          durable=True,
                          username=username,
                          password=password,
                          exchange=exchange))

# log
test_logger.error('python-logstash: test logstash error message.')
test_logger.info('python-logstash: test logstash info message.')
test_logger.warning('python-logstash: test logstash warning message.')

try:
  1/0
except:
  test_logger.exception('python-logstash: test logstash exception with stack trace')

不管怎么说,最后生成的格式是这样就可以了。

{
  "@source"=>"unknown",
  "@type"=>"nginx",
  "@tags"=>[],
  "@fields"=>{
    "remote_addr"=>"192.168.0.1",
    "remote_user"=>"-",
    "body_bytes_sent"=>"13988",
    "request_time"=>"0.122",
    "status"=>"200",
    "request"=>"GET /some/url HTTP/1.1",
    "request_method"=>"GET",
    "http_referrer"=>"http://www.example.org/some/url",
    "http_user_agent"=>"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1"
  },
  "@timestamp"=>"2012-08-23T10:49:14+02:00"
}

我这里简单提一下,这个模块用的不是很满意,我在python下把日志打成了json字符串,我原本以为会像grok那样,在Es里面,我的这条日志是个字段的结构,而不是这个日志都在message里面…. 我想大家应该明白了我的意思,这样很是不容易在kibana的搜索…

在kibana搜索,我经常上 source:xxx AND level:INFO 结果正像上面描述的那样,整条日志,都在@message里面。

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

(0)

相关推荐

  • Python 格式化打印json数据方法(展开状态)

    目的:Python 格式化打印json数据方法(展开状态) 环境: 系统:Win10 x64 环境: Pycharm Python 3.7.0 问题分析: Python下json手法的json在打印查看时,括号和数据都没有展开,查看起来比较困难. 解决方案: 使用json.dumps的separators设置,将数据展开格式化打印. 解决步骤: data = json.dumps(data, sort_keys=True, indent=4, separators=(', ', ': ')) 例

  • Python爬取数据保存为Json格式的代码示例

    python爬取数据保存为Json格式 代码如下: #encoding:'utf-8' import urllib.request from bs4 import BeautifulSoup import os import time import codecs import json #找到网址 def getDatas(): # 伪装 header={'User-Agent':"Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.1

  • python中json格式数据输出的简单实现方法

    主要使用json模块,直接导入import json即可. 小例子如下: #coding=UTF-8 import json info={} info["code"]=1 info["id"]=1900 info["name"]='张三' info["sex"]='男' list=[info,info,info] data={} data["code"]=1 data["id"]=190

  • python 串行执行和并行执行实例

    我就废话不多说了,大家还是直接看代码吧! #coding=utf-8 import threading import time import cx_Oracle from pprint import pprint import csv print time.asctime() table_name = "dbtest.csv" f = open(table_name + ".csv", "w") conn = cx_Oracle.connect(

  • Nodejs中怎么实现函数的串行执行

    nodejs实现函数串行执行 众所周知,nodejs是用非阻塞异步io机制,也就是说函数执行完成的先后顺序和代码的编写的先后顺序可能不同,实际的顺序取决于一个函数自身执行的快慢.这就出现一个在nodejs中怎么编写串行执行也就是顺序执行代码的问题.试想,要是你写了2个函数,一个查询数据库,另一个返回查询的结果,要是不用串行执行那么还没等数据库查询完毕就返回了结果,这显然是不行的.因而串行执行在nodejs中很有用.那么怎么实现呢? 大家可能会说,将返回查询结果的函数写在查询数据库函数的回调函数里

  • 解决Python 命令行执行脚本时,提示导入的包找不到的问题

    在Pydev能正常执行的脚本,在导出后在命令行执行,通常会报自己写的包导入时找不到. 一:报错原因 在PyDev中,test.py 中导入TestUserCase里面的py文件时,会写: from TestUserCase.Test import Test 这在命Pydev中没错,但是在命令行中确出现错误. 因为在PyDev中默认路径除了当前文件所在路劲外,还有工程根路径.而在命令行中只有当前文件所在路径.TestUserCase是test.py文件的上一级目录,所以在命令行中test.py i

  • Javascript异步流程控制之串行执行详解

    这篇文章主要讲一下如何串行执行一组异步任务,例如有下面几个任务,在这里我们用setTimeout模拟一个异步任务: let taskA = () => setTimeout(() => console.log('run task A'), 100); let taskB = () => setTimeout(() => console.log('run task B'), 50); let taskC = () => setTimeout(() => console.l

  • Java并发之串行线程池实例解析

    前言 做Android的这两年时间,通过研究Android源码,也会Java并发处理多线程有了自己的一些理解. 那么问题来了,如何实现一个串行的线程池呢? 思路 何为串行线程池呢? 也就是说,我们的Runnable对象应该有个排队的机制,它们顺序从队列尾部进入,并且从队列头部选择Runnable进行执行. 既然我们有了思路,那我们就考虑一下所需要的数据结构? 既然是从队列尾部插入Runnable对象,从队列头部执行Runnable对象,我们自然需要一个队列.Java的SDK已经给我们提供了很好的

  • Python并发之多进程的方法实例代码

    一,进程的理论基础 一个应用程序,归根结底是一堆代码,是静态的,而进程才是执行中的程序,在一个程序运行的时候会有多个进程并发执行. 进程和线程的区别: 进程是系统资源分配的基本单位. 一个进程内可以包含多个线程,属于一对多的关系,进程内的资源,被其内的线程共享 线程是进程运行的最小单位,如果说进程是完成一个功能,那么其线程就是完成这个功能的基本单位 进程间资源不共享,多进程切换资源开销,难度大,同一进程内的线程资源共享,多线程切换资源开销,难度小 进程与线程的共同点: 都是为了提高程序运行效率,

  • jQuery使用deferreds串行多个ajax请求

    使用JQuery对多个ajax请求串行执行. HTML代码: <a href="#">Click me!</a> <div></div> JS: function GetSomeDeferredStuff() { var deferreds = []; var i = 1; for (i = 1; i <= 10; i++) { var count = i; deferreds.push( $.post('/echo/html/',

  • 详解IOS串行队列与并行队列进行同步或者异步的实例

    详解IOS串行队列与并行队列进行同步或者异步的实例 IOS中GCD的队列分为串行队列和并行队列,任务分为同步任务和异步任务,他们的排列组合有四种情况,下面分析这四种情况的工作方式. 同步任务,使用GCD dispatch_sync 进行派发任务 - (void)testSync { dispatch_queue_t serialQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_SERIAL); dispatch_

  • python命令行参数用法实例分析

    本文实例讲述了python命令行参数用法.分享给大家供大家参考,具体如下: 在命令行下执行某些命令的时候,通常会在一个命令后面带上一些参数,这些参数会传递到程序里,进行处理,然后返回结果,在linux 下很多命令其实也是用python来实现的.那么如果做到在命令行输入类似如下的命令并能得到正确的解析呢,比如 python commandline.py -l c:/temp -f c:/aaa.txt 这样的命令,并在程序中进行处理.下面详细介绍处理办法. python为我们提供了一个模块叫opt

  • Python3 pickle对象串行化代码实例解析

    1.pickle对象串行化 pickle模块实现了一个算法可以将任意的Python对象转换为一系列字节.这个过程也被称为串行化对象.可以传输或存储表示对象的字节流,然后再重新构造来创建有相同性质的新对象. 1.1 编码和解码字符串中的数据 第一个例子使用dumps()将一个数据结构编码为一个字符串,然后把这个字符串打印到控制台.它使用了一个完全由内置类型构成的数据结构.任何类的实例都可以pickled,如后面的例子所示. import pickle import pprint data = [{

随机推荐