printbob.py:
import sys
for arg in sys.argv:
print arg
Run Code Online (Sandbox Code Playgroud)
getbob.py
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 (commands are seperated by spaces)
print subprocess.Popen(['printbob.py', 'arg1 arg2 arg3 arg4']).wait()
x = raw_input('done')
Run Code Online (Sandbox Code Playgroud)
我明白了:
File "C:\Python27\lib\subprocess.py", line 672, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 882, in _execute_child
startupinfo)
WindowsError: [Error 193] %1 is not a valid Win32 application
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?我只想在另一个python脚本中获取另一个python脚本的输出.我是否需要调用cmd.exe或者我可以运行printbob.py并向其发送命令吗?
我有一些代码需要每10秒写一次大约20个字节的数据.我在Windows 7上使用python 2.7
你们建议任何"os/hard drive"压力最小的方法吗?
我在考虑打开和关闭同一个文件10秒钟:
f = open('log_file.txt', 'w')
f.write(information)
f.close()
Run Code Online (Sandbox Code Playgroud)
或者我应该保持打开只是flush()数据而不是经常关闭它?
怎么样sqllite?它是否会提高性能并且不如打开和关闭文件操作那么密集?(它不仅仅是一个平面文件数据库所以==到文本文件反正......?)
怎么样mysql(这使用本地服务器/进程..不确定何时/如何将数据保存到硬盘驱动器的细节)?
我只是担心不会煎炸我的硬盘并提高这个日志记录程序的性能.我将每隔10秒钟收到一次新的日志信息,这将全天24小时全天候开放.你的建议?
即:考虑像utorrent这样的程序需要长时间保存大量数据,(我的日志文件显着少于那些用像utorrent这样的"下载程序类型程序"编写的数据)
import random
import time
def get_data():
letters = 'isn\'t the code obvious'
data = ''
for i in xrange(20):
data += random.choice(letters)
return data
while True:
f = open('log_file.txt', 'w')
f.write(get_data())
f.close()
time.sleep(10)
Run Code Online (Sandbox Code Playgroud)
我的CPU在大约15秒后开始抱怨......(或者那是我的硬盘?)
我已经阅读了很多关于锁定线程的例子..但为什么要锁定它们呢?根据我的理解,当您启动线程而不加入它们时,它们将与主线程和所有其他线程竞争资源,然后执行,有时同时执行,有时不执行.
锁定是否确保线程不会同时执行?
同时,线程执行同步有什么问题?那不是更好吗?(整体执行速度更快)
当您锁定线程时,它会锁定它们还是您可以选择要锁定的线程?(无论锁定实际上是什么......)
我指的是使用lock()这样的锁函数并在线程模块中获取btw ...
我已经阅读了很多关于使用线程,子进程等的帖子.对于我正在尝试做的事情,很多事情似乎过于复杂......
我想要做的就是在X时间过后停止执行一个函数.
def big_loop(bob):
x = bob
start = time.time()
while True:
print time.time()-start
Run Code Online (Sandbox Code Playgroud)
此函数是一个无限循环,永远不会抛出任何错误或异常,句点.我不确定"命令,shell,子进程,线程等......"和这个函数之间的区别,这就是我在操作子进程时遇到问题的原因.
我在这里找到了这个代码,然后尝试了它,但是你可以看到它在10秒后继续打印:
import time
import threading
import subprocess as sub
import time
class RunCmd(threading.Thread):
def __init__(self, cmd, timeout):
threading.Thread.__init__(self)
self.cmd = cmd
self.timeout = timeout
def run(self):
self.p = sub.Popen(self.cmd)
self.p.wait()
def Run(self):
self.start()
self.join(self.timeout)
if self.is_alive():
self.p.terminate()
self.join()
def big_loop(bob):
x = bob
start = time.time()
while True:
print time.time()-start
RunCmd(big_loop('jimijojo'), 10).Run() #supposed to quit after 10 seconds, but doesn't
x = raw_input('DONEEEEEEEEEEEE')
Run Code Online (Sandbox Code Playgroud)
这个功能被杀死的简单方法是什么.正如你在上面的尝试中所看到的那样,它在20秒后不会终止并继续......
***哦,我也读过有关使用信号的内容,但是我在Windows上,所以我不能使用闹钟功能..(python …
from threading import Thread
import time
print 'start of script'
class MyThread(Thread):
def __init__(self, start, end):
self.start = start
self.end = end
def run(self):
for i in xrange(self.start,self.end):
yield i
my_threads = []
my_thread = MyThread(1,6)
my_thread.start()
my_threads.append(my_thread)
my_thread = MyThread(6,11)
my_thread.start()
my_threads.append(my_thread)
my_thread = MyThread(11,16)
my_thread.start()
my_threads.append(my_thread)
for t in my_threads:
print t.join()
print 'end of script'
Run Code Online (Sandbox Code Playgroud)
我怎样才能正确地做到这一点?我正在尝试打印数字:范围(1,16),其中我从在单独线程中运行的函数的输出中获取该数字。
我知道我不会按顺序获得这个数字范围,因为函数的本质是在单独的线程中运行。
我也知道我可以简单地在线程函数本身中打印它们,但这不是重点,我想打印我在主线程或代码的主要部分中产生的内容。
from threading import Thread
import time
print 'start of script'
class MyThread(Thread):
def run(self):
for i in xrange(10):
print 'thread->', '['+self.name+']', '('+str(i)+')'
time.sleep(2)
for i in range(3):
my_thread = MyThread()
my_thread.name = i
my_thread.start()
print 'end of script'
>>> ================================ RESTART ================================
>>>
start of script
thread->thread->thread->end of script
[0][1][2]
>>> (0)(0)(0)
thread->thread->thread-> [0][2][1] (1)(1)(1)
thread->thread-> thread-> [0] [2] [1] (2) (2)
(2)
thread-> thread->thread->[0] [2][1](3)
(3)(3)
thread-> thread->thread->[0] [2][1](4)
(4)(4)
thread-> thread->[0]thread-> [2](5)[1]
(5)(5)
thread-> [0]thread->thread-> (6)[2][1]
(6)(6)
thread-> …Run Code Online (Sandbox Code Playgroud) 我创建了一个函数timeout_func,它将正常运行另一个函数并返回其输出,但如果函数超过'secs',它将返回一个字符串'failed'.基本上,它是一个解决方法来超时可以无限运行的函数.(Windows上的python 2.7)(为什么我需要一个解决方法,为什么我不能让函数非无限?因为有时候你不能这样做,它在已知的进程中称为错误,即:fd = os.open('/dev/ttyS0', os.O_RDWR)
无论如何,我的timeout_func受到了我在这里收到的帮助的启发: 在Windows中经过一段时间后杀死一个函数
我的代码的问题是,由于某种原因,该do_this函数接收14个变量而不是一个.通过双击脚本或从python.exe运行它时,我得到错误消息.从IDLE你没有异常错误....

但是,如果我将其更改为:
def do_this(bob, jim):
return bob, jim
Run Code Online (Sandbox Code Playgroud)
它工作得很好......
这里发生了什么?它不喜欢1变量函数......?
import multiprocessing
import Queue
def wrapper(queue, func, func_args_tuple):
result = func(*func_args_tuple)
queue.put(result)
queue.close()
def timeout_func(secs, func, func_args_tuple):
queue = multiprocessing.Queue(1) # Maximum size is 1
proc = multiprocessing.Process( target=wrapper, args=(queue, func, func_args_tuple) )
proc.start()
# Wait for TIMEOUT seconds
try:
result = queue.get(True, secs)
except Queue.Empty:
# Deal with lack of data somehow
result …Run Code Online (Sandbox Code Playgroud) l = [
{'bob':'hello','jim':'thanks'},
{'bob':'world','jim':'for'},
{'bob':'hey','jim':'the'},
{'bob':'mundo','jim':'help'}
]
for dict in l:
print dict['jim']
Run Code Online (Sandbox Code Playgroud)
有这样的单线或pythonic方式吗?我正在尝试检索词典列表中只有1个项目的列表