Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interrealm: Add option to run the bridge unidirectionally. #459

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rht
Copy link
Contributor

@rht rht commented Jul 25, 2018

This is for the use case of playground.zulipdev.org.

@zulipbot
Copy link
Member

Heads up @rht, we just merged some commits that conflict with the changes your made in this pull request! You can review this repository's recent commits to see where the conflicts occur. Please rebase your feature branch against the upstream/master branch and resolve your pull request's merge conflicts accordingly.

@timabbott
Copy link
Member

@neiljp can you take a look at this?

p2.start()
print("Listening...")
p1.join()
p2.join()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this without duplicating code between the two paths?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the simplest way to do it without having to add more lines.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is cleaner to read, but if this is equivalent then you could have:

    1 if not args.oneway:
    2     pipe_event2 = ...
    3     p2 = mp.Process(...)
    4     p2.start()
    5 p1.start()
    6 print("Listening")
    7 p1.join()
    8 if not args.oneway
    9     p2.join()

Does the start() order matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it is possible to do it that way too.

Copy link
Contributor

@neiljp neiljp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this could be achieved more cleanly by using concurrent.futures?

p2.start()
print("Listening...")
p1.join()
p2.join()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is cleaner to read, but if this is equivalent then you could have:

    1 if not args.oneway:
    2     pipe_event2 = ...
    3     p2 = mp.Process(...)
    4     p2.start()
    5 p1.start()
    6 print("Listening")
    7 p1.join()
    8 if not args.oneway
    9     p2.join()

Does the start() order matter?

@rht
Copy link
Contributor Author

rht commented Jan 19, 2019

@neiljp how does a concurrent.futures version look like? If it is cleaner, maybe it should be used in the other bridges.

@neiljp
Copy link
Contributor

neiljp commented Jan 19, 2019

@rht I haven't explored it in detail for this application, but it could perhaps be something like the following (untested):

    from concurrent.futures import ProcessPoolExecutor, wait
    pipe_event1 = create_pipe_event(client2, bot1, bot2, args.stream)
    with ProcessPoolExecutor(max_workers=2) as executor:
        f = [executor.submit(client1.call_on_each_event, pipe_event1, ["message"])]
        if not args.oneway:
            pipe_event2 = create_pipe_event(client1, bot2, bot1, args.stream)
            f += [executor.submit(client2.call_on_each_event, pipe_event2, ["message"])]
        print("Listening...")
        wait(f)

I used this module in zulip-terminal recently.

@rht
Copy link
Contributor Author

rht commented Jan 19, 2019

That looks like a bit longer than I thought (concurrent.futures as a common interface to multiprocessing and threading). Would it be possible to do

f1 = executor.submit(client1.call_on_each_event, pipe_event1, ["message"])
f1.future()

?
Even if this is possible, there is still create_pipe_event construct, which is another extra structure.

@neiljp
Copy link
Contributor

neiljp commented Jan 20, 2019

Longer? The original is 13 lines, and the new one is 8, plus the import line - plus cleaner, in my opinion, though no guarantee it's directly equivalent as I'm not completely familiar with the existing code.

Re your question, f1 is already a future; in my draft code above, the futures are just being used to test the completion state of the code, which by my understanding is equivalent to a join. I found the library docs quite readable - assuming I understand them, of course ;)

Refactoring for the other structures is a separate step, which I hadn't considered.

@rht
Copy link
Contributor Author

rht commented Jan 20, 2019

"Longer" in a horizontal way rather than vertical way, in terms of the number of nested expressions.
(Also, I somehow forgot the fact that create_pipe_event was a construct in the bridge itself.)
Also, typo: f1.result() instead of f1.future().

Aside: maybe they (anything that runs n functions in parallel) could be wrapped in a run_parallel so that no one has to see the gory detail of the parallelization?

run_parallel((func1, args1), (func2, args2), ...)

There is a run_parallel function in zulip/zulip, but it uses os process forks.

@neiljp
Copy link
Contributor

neiljp commented Jan 20, 2019

The extra level of nesting is due to that context manager, which tidies things up automatically - does the same happen currently?

You could get a result by f1.result(), but currently the result of call_on_each_event would be None, of course; I think it would probably block at that point, too.

@zulipbot
Copy link
Member

Heads up @rht, we just merged some commits that conflict with the changes your made in this pull request! You can review this repository's recent commits to see where the conflicts occur. Please rebase your feature branch against the upstream/master branch and resolve your pull request's merge conflicts accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants