如何在Python3.3中使用fork()
如何在Python3.3中使用fork() **这是我的代码: 输入:如何在Python3.3中使用fork(),python,windows,Python,Windows,如何在Python3.3中使用fork() **这是我的代码: 输入: #!/usr/bin/env python import os def Child_process(): print("We are in Child_Process") print("My PID: %d"%os.getpid()) print("Child_Process is exiting") def Parent_process(): print("-------Parent_p
#!/usr/bin/env python
import os
def Child_process():
print("We are in Child_Process")
print("My PID: %d"%os.getpid())
print("Child_Process is exiting")
def Parent_process():
print("-------Parent_process---------")
wpid = os.fork()
if wpid==0:
print("wpid is 0 means We are in Child_process")
print("Child :%d"%wpid)
Child_process()
else:
print("Execute Parent_process")
print("Parent_process %d"%wpid)
Parent_process()
Parent_process()
输出:
C:\Python33\python.exe C:/Users/Iem-Prog/Desktop/Py/Fork
Traceback (most recent call last):
File "C:/Users/Iem-Prog/Desktop/Py/Fork", line 21, in <module>
-------Parent_process---------
Parent_process()
File "C:/Users/Iem-Prog/Desktop/Py/Fork", line 11, in Parent_process
wpid = os.fork()
AttributeError: 'module' object has no attribute 'fork'
C:\Python33\python.exe C:/Users/Iem Prog/Desktop/Py/Fork
回溯(最近一次呼叫最后一次):
文件“C:/Users/Iem Prog/Desktop/Py/Fork”,第21行,在
-------父进程---------
父进程()
文件“C:/Users/Iem Prog/Desktop/Py/Fork”,第11行,在父进程中
wpid=os.fork()
AttributeError:“模块”对象没有属性“fork”
仅在类Unix系统中可用。你不能在Windows中使用它
os.fork()
派生一个子进程。在子进程和子进程id中返回0
在父母身上。如果发生错误,将引发OSError
请注意,包括FreeBSD在内的一些平台仅在类Unix系统中可用。你不能在Windows中使用它
os.fork()
派生一个子进程。在子进程和子进程id中返回0
在父母身上。如果发生错误,将引发OSError
<> P>注意,FreeBSD
自代码< > OS.Office < /C> >的一些平台在您的目标上不可用,而是考虑使用“甚至”(不包括电池)。
这些在创建孩子时创建了一个方便的抽象。
< P>因为这些创建了一个方便的关于启动子项的抽象。您应该使用python的默认值。它同时适用于Linux和Windows
from multiprocessing import Process, Array
def split_work_receiver(import_type, process_no, i, shared_arr):
creds= login()
if creds is not None:
process_submissions(browser, i, import_type, process_no, shared_arr)
else:
print("Failed login.")
return
def split_work(base_path, i, connection_url, database_name):
shared_arr = Array('i', range(0)) # Used to send data across processeses
processes = [
Process(target=split_work_receiver, args=("arg1", base_path, i, shared_arr)),
Process(target=split_work_receiver, args=("arg1", base_path, i, shared_arr)),
Process(target=split_work_receiver, args=("arg1", base_path, i, shared_arr))]
#Run processes
for p in processes:
p.start()
while True:
sleep(600)
#Exit the completed processes
for p in processes:
print('Closed process: '+ str(p))
p.join()
您应该使用python的默认值。它同时适用于Linux和Windows
from multiprocessing import Process, Array
def split_work_receiver(import_type, process_no, i, shared_arr):
creds= login()
if creds is not None:
process_submissions(browser, i, import_type, process_no, shared_arr)
else:
print("Failed login.")
return
def split_work(base_path, i, connection_url, database_name):
shared_arr = Array('i', range(0)) # Used to send data across processeses
processes = [
Process(target=split_work_receiver, args=("arg1", base_path, i, shared_arr)),
Process(target=split_work_receiver, args=("arg1", base_path, i, shared_arr)),
Process(target=split_work_receiver, args=("arg1", base_path, i, shared_arr))]
#Run processes
for p in processes:
p.start()
while True:
sleep(600)
#Exit the completed processes
for p in processes:
print('Closed process: '+ str(p))
p.join()
你使用哪种操作系统?例如,在windows下,这是不可能的。请调查
子进程
和多处理
模块。幸好它不起作用,因为它会无限地调用自身并成为一个分叉炸弹。您使用哪种操作系统?例如,在windows下,这是不可能的。请调查子进程
和多处理
模块。幸好它不起作用,因为它会无限地调用自身并成为一个分叉炸弹。