GVKun编程网logo

python 的进程与线程(二)(python中的进程和线程)

2

这篇文章主要围绕python的进程与线程和二展开,旨在为您提供一份详细的参考资料。我们将全面介绍python的进程与线程的优缺点,解答二的相关问题,同时也会为您带来/System/Library/Fr

这篇文章主要围绕python 的进程与线程展开,旨在为您提供一份详细的参考资料。我们将全面介绍python 的进程与线程的优缺点,解答的相关问题,同时也会为您带来/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python:无法打开文件“esptool.py”、CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取、Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod、Error: Can‘t find Python executable “python“, you can set the PYTHON env variable的实用方法。

本文目录一览:

python 的进程与线程(二)(python中的进程和线程)

python 的进程与线程(二)(python中的进程和线程)

线程

        之前了解了操作系统的发展史,也知道了进程和线程的概念,归纳一下就是:

        进程:本质上就是一段程序的运行过程(抽象的概念)

        线程:最小的执行单元,是进程的实体

        进程:最小的资源单位
 

线程的调用

        在 python 中,一般通过导入 threading 模块来调用线程。 threading 模块建立在 thread 模块之上。thread 模块以低级、原始的方式来处理和控制线程,而 threading 模块通过对 thread 进行二次封装,提供了更方便的 api 来处理线程,先看一段直接调用的代码:

       直接调用:     

# -*- coding: utf-8 -*-
import threading,time

def func1(n):
    print(''传给我的是%s''%n)
    time.sleep(3)

def func2():
    print(''我没有参数的'')
    time.sleep(5)

if __name__ == ''__main__'':
    t1 = threading.Thread(target=func1,args=(5,))   #target传入的是函数名,不带括号,args要以元组形式传入参数
    t2 = threading.Thread(target=func2)   #没有参数就不用传

    t1.start()    #运行线程,本质是调用run方法
    t2.start()
    print(''ending......'')
    
>>>传给我的是5
>>>我没有参数的
>>>ending......
View Code

        运行的时候几乎同时打印了三条结果,然后等待约 5s 再停止,如果是单线程的话就要 8s 的时间才能运行完,这样做提升了效率。其次这个程序一共有多少线程呢?答案是 3 个,除了 t1 和 t2 两个实例化得到的子线程,还有程序本身的主线程,还记得那句话吗?一个程序至少有一个进程,一个进程至少有一个线程。

        继承方式调用线程: 

import time
import threading

class Mythread(threading.Thread):
    def __init__(self,n):
        threading.Thread.__init__(self)
        self.n = n

    def func1(self):
        print(''传给我的是%s''%self.n)
        time.sleep(self.n)

    def run(self):   ##定义每个线程要运行的函数
        self.func1()

if __name__ == ''__main__'':
    t1 = Mythread(2)
    t2 = Mythread(5)

    t1.start()
    t2.start()

>>>传给我的是2
>>>传给我的是5
View Code

 

       也是直接打印两条结果,然后等待 5s 程序结束。一般这种方式很少用到,都是直接调用比较快。如果不穿参数的话,__init__方法可以不用写,但是 run 函数一定要写的,至于为什么,你可以通过找继承的父类去推,这里就不啰嗦了。

       线程的 join 方法

     join 是线程实例化后也具有的方法,像上边的 start 一样,那么 join 有什么作用呢?

 

import threading
from time import ctime,sleep

def music(name):

        print ("Begin listening to %s. %s" %(name,ctime()))
        sleep(2)
        print("end listening %s"%ctime())


def moive(title):

        print ("Begin recording the %s! %s" %(title,ctime()))
        sleep(5)
        print(''end recording %s''%ctime())

threads = []

t1 = threading.Thread(target=ListenMusic,args=(''毛不易的歌'',))
t2 = threading.Thread(target=RecordBlog,args=(''白蛇'',))

threads.append(t1)
threads.append(t2)

if __name__ == ''__main__'':

    for t in threads:
        t.start()
        #t.join()#串行
    # t.join()

    # t1.join()

    # t2.join()########分别试一下这几种join位置下的结果
    print(''endtime is %s''%ctime())
View Code

 

      我们逐个分析,不加 join 时:

Begin listening to 毛不易的歌. Thu Apr 11 17:47:01 2019
Begin recording the 白蛇! Thu Apr 11 17:47:01 2019
endtime is Thu Apr 11 17:47:01 2019
end listening Thu Apr 11 17:47:04 2019
end recording Thu Apr 11 17:47:06 2019

       加上一个 t1.join 时:

Begin listening to 毛不易的歌. Thu Apr 11 17:49:42 2019
Begin recording the 白蛇! Thu Apr 11 17:49:42 2019
end listening Thu Apr 11 17:49:45 2019
endtime is Thu Apr 11 17:49:45 2019
end recording Thu Apr 11 17:49:47 2019

       注意 endtime 的变化,然后去试试其他情况就可以知道(注意 for 循环里面和外面的 t 的区别),join 作用就是在子线程完成运行之前,这个子线程的父线程将一直被阻塞。

       线程的 setDaemon 方法

      通过 join 我们可以阻塞线程,那么 setDaemon 就刚好相反,就是将线程声明为守护线程,但是必须在 start () 方法调用之前设置, 如果不设置为守护线程程序会被无限挂起。当我们 在程序运行中,执行一个主线程,如果主线程又创建一个子线程,主线程和子线程 就分兵两路,分别运行,那么当主线程完成想退出时,会检验子线程是否完成。如 果子线程未完成,则主线程会等待子线程完成后再退出。但是有时候我们需要的是 只要主线程完成了,不管子线程是否完成,都要和主线程一起退出,这时就可以 用 setDaemon 方法啦

import threading
from time import ctime,sleep

def music(name):

        print ("Begin listening to %s. %s" %(name,ctime()))
        sleep(2)
        print("end listening %s"%ctime())


def moive(title):

        print ("Begin recording the %s! %s" %(title,ctime()))
        sleep(5)
        print(''end recording %s''%ctime())

threads = []

t1 = threading.Thread(target=ListenMusic,args=(''毛不易的歌'',))
t2 = threading.Thread(target=RecordBlog,args=(''白蛇'',))

threads.append(t1)
threads.append(t2)

if __name__ == ''__main__'':
    #t1.setDaemon(True)
    t2.setDaemon(True)
    for t in threads:
        #t.setDaemon(True)   #一定要在start前
        t.start()
        #t.join()#串行
    # t.join()

    t1.join()

    # t2.join()########分别试一下这几种join位置下的结果
    print(''endtime is %s''%ctime())
View Code

        看一下结果,白蛇没看完就停了

Begin listening to 毛不易的歌. Thu Apr 11 18:03:42 2019
Begin recording the 白蛇! Thu Apr 11 18:03:42 2019
end listening Thu Apr 11 18:03:45 2019
endtime is Thu Apr 11 18:03:45 2019

       其他方法

# run():  线程被cpu调度后自动执行线程对象的run方法
# start():启动线程活动。
# isAlive(): 返回线程是否活动的。
# getName(): 返回线程名。
# setName(): 设置线程名。

threading模块提供的一些方法:
# threading.currentThread(): 返回当前的线程变量。
# threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
# threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。

       基本的线程就是这样使用的,后面讲的就是同步和异步的概念,还有各种锁,慢慢来。

 

 

 

 

 

 

  

原文出处:https://www.cnblogs.com/pengfy/p/10691131.html

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python:无法打开文件“esptool.py”

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python:无法打开文件“esptool.py”

如何解决/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python:无法打开文件“esptool.py”

我正在 mac OS X 上使用 ESP8266Flash.app 更新 ESP8266 固件。 但是当我开始刷固件时出现以下错误:

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can''t open file ''esptool.py'': [Errno 1] Operation not permitted."

我该如何解决这个问题?

CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取

CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取

如何解决CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取

我有一个看起来像这样的 CMake 脚本:

  1. find_program(PYTHON_COMMAND NAMES python3 python)

问题是它检测到安装在 Cygwin 安装中的 python。 输出总是:

  1. -- PYTHON_PATH:C:/cygwin64/bin/python3

我希望它取自:

  1. c:\\python36-64\\python

在windows PATH变量中,Cygwin bin在路径的最后一个,windows安装在第一个 但它只检测到 Cygwin python,
怎么改?

Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod

Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod

It's important to understand the Python execution model and precisely when function deFinitions and other important events occur when a module is imported or executed. Here, we show execution of our Python module as it's imported in a graphical debugging environment. We step through the top‑level statements in the module. What's important to realize here is that the def used for the fetch_words function isn't merely a declaration. It's actually a statement, which when executed in sequence with the other top‑level model scope code, causes the code within the function to be bound to the name of the function. When modules are imported or run, all of the top‑level statements are run, and this is the means by which the function within the module namespace are defined. We are sometimes asked about the difference between Python modules, Python scripts, and Python programs. Any .py file constitutes a Python module. But as we've seen, modules can be written for convenient import, convenient execution, or using the if dunder name = dunder main idiom, both. We strongly recommend making even simple scripts importable since it eases development and testing so much if you can access your code from within the REPL. Likewise, even modules, which are only ever meant to be imported in production settings, benefit from having executable test code. For this reason, nearly all modules we create have this form of defining one or more importable functions with a postscript to facilitate execution. Whether you consider a module to be a Python script or Python program is a matter of context and usage. It's certainly wrong to consider Python to be merely a scripting tool, in the vein of Windows batch files or UNIX Shell scripts, as many large and complex applications are built exclusively with python.

 

  • def不仅仅是一个declaration声明,更是一条statement语句。它将其中的python代码于函数名绑定在一起
  • 一个py文件就是一个模块,这个模块包含类或函数。你写python,要尽量将代码包装成函数和类,方便各种import
  • 一个py文件也可看作是一个脚本,在系统命令行中运行
  • python不仅仅是脚本语言,很多大型程序都是用python构建的

Error: Can‘t find Python executable “python“, you can set the PYTHON env variable

Error: Can‘t find Python executable “python“, you can set the PYTHON env variable

在启动vue项目的时候,安装node.js组件node-sass过程中报错了,错误提示如下
Error: Can’t find Python executable “python”, you can set the PYTHON env variable

由错误提示可知:Node.js 在安装模块组件node-sass的时候,node.js缺少Visual Studio2015 Build Tools相关的组件和python的环境,如果安装了vs2015组件的小伙伴们就不用安装Visual Studio2015 Build Tools相应的组件,只用安装python2.7即可解决缺少的python组件的问题。

欲安装python2.7,请至python官网:www.python.org 下载,然后配置好python的环境变量即可。

不过博主我并不推荐上述的解决方案,因为对于程序员来说,效率第一,上述的问题一个命令就可以轻松解决你所遇到的麻烦,前面说了那么多,无非就是想告诉在看本篇博客的同仁们放下浮躁的心,遇到问题首先不是急着去解决问题,而是分析为什么会这样,然后才能水到聚成的去找到解决问题的方法。

运行下面这个命令即可解决你们遇到的Error问题

npm install --global --production windows-build-tools

:上面讲述了一堆就是为了讲述此命令是干嘛的,上面已经描述很详细了,就不再赘述了,该操作与上述的一堆操作无异,效果却是一样的。

然后运气不好的小伙伴可能接着会遇到一个坑,那就是执行了:npm install --global --production windows-build-tools这个命令的人细心点会发现执行到一半就卡住了,这个卡住了没有红字重点提示,而且下方还有英文在等待中,粗心的小伙伴可能以为是命令执行完了,组件安装好了,其实不然,我这边已经解决了,就无法复现了,具体点就是中文的提示,提示我们由于有类似组件在运行或者下载导致无法继续下载安装组件了。稳妥点的解决办法是,将电脑重启,将底层正在运行的模块干掉,待电脑重启后再执行npm install --global --production windows-build-tools这条命令即可,博主我就是这样解决的,稳稳的幸福就会浮现在你面前如下图所示,你的可能和我不一样,因为我已经跑成功过一次了,没有你的那么多细节的log打印。
在这里插入图片描述

然后就是在你的项目下shift+鼠标右击你的项目运行npm run dev即可启动vue项目了。

关于python 的进程与线程的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python:无法打开文件“esptool.py”、CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取、Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod、Error: Can‘t find Python executable “python“, you can set the PYTHON env variable等相关知识的信息别忘了在本站进行查找喔。

本文标签: