本文整理汇总了Python中multiprocessing.Pipe.send_bytes方法的典型用法代码示例。如果您正苦于以下问题:Python Pipe.send_bytes方法的具体用法?Python Pipe.send_bytes怎么用?Python Pipe.send_bytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.Pipe
的用法示例。
在下文中一共展示了Pipe.send_bytes方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_multiprocessing_pipe
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import send_bytes [as 别名]
def test_multiprocessing_pipe(self):
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,))
p.start()
for _ in range(5):
while parent_conn.poll():
print("Got from client", parent_conn.recv_bytes()) # prints "[42, None, 'hello']"
time.sleep(1)
parent_conn.send_bytes(b"stop")
p.join()
示例2: main
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import send_bytes [as 别名]
def main(which="PipedService"):
print "main(): pid = {pid}, ppid = {ppid}".format(pid=os.getpid(), ppid=os.getppid())
if which == "PipedService":
print "main(): Starting PipedService process..."
pipeToProc, pipeToMain = Pipe()
proc = PipedService(pipeToMain)
proc.start()
sleep(1) # [debug] wait a bit to flush out all messages from child processes
proc.test() # [debug] test self and parent PIDs
while proc.is_alive():
command = raw_input("main(): Command : ")
if proc.is_alive():
pipeToProc.send_bytes(command)
print "main(): Response: {0}".format(pipeToProc.recv_bytes())
else:
print "main(): Oops! Process already died."
print "main(): Done; joining on process(es)..."
proc.join()
elif which == "QueuedService":
print "main(): Starting QueuedService child process..."
service = QueuedService()
service.start()
print "main(): Starting cannedLoop() child process..."
cannedCommands = ["Hi", "How", "is", "it going?", "quit"]
pCannedLoop = Process(target=cannedLoop, args=(service, cannedCommands, 5))
pCannedLoop.start()
print "main(): Starting interactiveLoop() (NOTE: Not a child process)..."
interactiveLoop(service)
print "main(): Joining on process(es)..."
pCannedLoop.join()
service.join()
print "main(): Done."
elif which == "MultiPipedService":
print "main(): Starting MultiPipedService child process..."
service = MultiPipedService()
serviceHelper1 = service.addClient()
serviceHelper2 = service.addClient()
service.start() # NOTE must addClient()s before calling start()
sleep(1) # let other process start-up messages to pass through
print "main(): Starting cannedLoop() child process..."
cannedCommands = ["Hi", "How", "is", "it going?", "quit"]
pCannedLoop = Process(target=cannedLoop, args=(serviceHelper1, cannedCommands, 2))
pCannedLoop.start()
sleep(1) # let other process start-up messages to pass through
print "main(): Starting interactive loop..."
while True:
command = raw_input("Command > ")
if not service.is_alive():
print "main(): Oops! Service already dead; aborting..."
break
response = serviceHelper2.runCommandSync(command)
print "Response: {0}".format(response)
if command == "quit":
break
print "main(): Interactive loop terminated."
print "main(): Joining on process(es)..."
pCannedLoop.join()
service.join() # MultiPipedService automatically closes client connections on quit
print "main(): Done."
elif which == "SynchronizedService":
print "main(): Starting SynchronizedService child process..."
service = SynchronizedService()
service.start()
sleep(1) # let other process start-up messages to pass through
print "main(): Starting cannedLoop() child process..."
cannedCommands = ["Hi", "How", "is", "it going?", "quit"]
pCannedLoop = Process(target=cannedLoop, args=(service, cannedCommands, 2))
pCannedLoop.start()
sleep(1) # let other process start-up messages to pass through
print "main(): Starting interactive loop..."
while True:
command = raw_input("Command > ")
if not service.is_alive():
print "main(): Oops! Service already dead; aborting..."
break
response = service.runCommandSync(command)
print "Response: {0}".format(response)
if command == "quit":
break
print "main(): Interactive loop terminated."
print "main(): Joining on process(es)..."
pCannedLoop.join()
service.join() # MultiPipedService automatically closes client connections on quit
print "main(): Done."
else:
print "main(): Unknown service type \"{0}\"".format(which)
示例3: Pipe
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import send_bytes [as 别名]
#!/usr/bin/env python
from multiprocessing import Pipe
a, b = Pipe()
# a.send([1, 'hello', None])
# b_recv = b.recv()
# print 'b_recv:'
# print b_recv
#
# b.send_bytes('thank you')
# a_recv = a.recv_bytes()
# print 'a_recv:'
# print a_recv
import array
arr1 = array.array('i', range(5))
arr2 = array.array('i', [0] * 10)
# print 'arr1:'
# print arr1.itemsize
# print len(arr1)
# print 'arr2:'
# print arr2.itemsize
# print len(arr2)
a.send_bytes(arr1)
count = b.recv_bytes_into(arr2)
print 'count:'
print count
assert count == len(arr1) * arr1.itemsize
print arr2
示例4: Pipe
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import send_bytes [as 别名]
from multiprocessing import Pipe
snd,rcv = Pipe()
snd.send_bytes("1234");
print (rcv.recv_bytes())
示例5: IrcPlugin
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import send_bytes [as 别名]
class IrcPlugin(WillPlugin):
def __init__(self):
super(IrcPlugin, self).__init__()
self.pipe, child_pipe = Pipe()
self.irc_process = Process(target=self.irc_thread, args=(child_pipe,))
self.irc_process.start()
@hear(r"^(?P<text>.+)$", multiline=True)
def on_message(self, message, text=None):
room_name = self.get_room_from_message(message)["name"]
if '\n' in text:
self.say('Unable to send multiline messages to IRC', message=message, color='red')
return
self.send_command_irc_process(
COMMAND_MESSAGE, room_name, format_message(sender=message.sender.nick, body=text)
)
def send_command_irc_process(self, command, room_name, argument=None):
self.pipe.send_bytes(self.string_encode_message(command, room_name, argument))
def string_encode_message(self, command, room_name, argument=None):
return '{cmd}|{room_name};{arg}'.format(
cmd=command,
room_name=room_name,
arg=(argument or '')
)
@respond_to(r"^[cC]onnect to irc channel (?P<url>.+)")
def connect_to_channel(self, message, url):
room_name = self.get_room_from_message(message)["name"]
configuration = self.load(STORAGE_KEY, {})
if room_name in configuration:
self.say(
"This room is already connected to the channel: {}".format(
configuration[room_name]
),
message=message
)
configuration[room_name] = url
self.save(STORAGE_KEY, configuration)
self.send_command_irc_process(
COMMAND_CONNECT, room_name, url
)
@respond_to(r"^[dD]isconnect from irc")
def disconnect_from_channel(self, message):
room_name = self.get_room_from_message(message)["name"]
configuration = self.load(STORAGE_KEY, {})
if room_name in configuration:
del configuration[room_name]
self.save(STORAGE_KEY, configuration)
self.send_command_irc_process(
COMMAND_DISCONNECT, room_name
)
def irc_thread(self, pipe):
configuration = self.load(STORAGE_KEY, {})
bot = IrcBot()
bot.register_message_handler(self.send_to_hipchat_from_irc)
bot.connect_to_multiple(configuration)
for connection in bot.connections.values():
self.send_connection_notification(connection)
while True:
try:
bot.reactor.process_once(timeout=REACTOR_LOOP_TIMEOUT)
if pipe.poll():
encoded_message = pipe.recv_bytes()
command, room_name, argument = self.decode_string_message(encoded_message)
if command == COMMAND_CONNECT:
connection = bot.connect_to_url(room_name, argument)
self.send_connection_notification(connection)
elif command == COMMAND_MESSAGE:
bot.send_public_message(room_name, argument)
elif command == COMMAND_DISCONNECT:
bot.disconnect(room_name)
room = self.get_room_from_name_or_id(room_name)
self.say("Disconnected from IRC", room=room)
except Exception:
logging.critical('Error managing IRC connection', exc_info=True)
def decode_string_message(self, encoded_message):
command, payload = encoded_message.split('|', 1)
split_payload = payload.split(';', 1)
room_name = split_payload[0]
if len(split_payload) > 1:
argument = split_payload[1]
else:
argument = None
return command, room_name, argument
def send_to_hipchat_from_irc(self, connection, sender, message_text):
room = self.get_room_from_name_or_id(connection.name)
#.........这里部分代码省略.........